如何使用 Django South 为现有应用程序创建新数据库并设置默认值?

发布于 2024-10-01 14:39:41 字数 1646 浏览 3 评论 0原文

我正在处理一个使用 South 来迁移数据库的操作系统项目。 我正在从头开始构建一个新数据库,我想确保 South 已设置,以便将来可以轻松更新数据库。

看来这个过程应该是:

  1. 创建数据库
  2. 同步
  3. 数据库迁移

但是,当我尝试迁移数据库时,早期的迁移之一(迁移0004,它们转到0009)抛出异常:

ValueError: You cannot add a null=False column without a default value.

我不明白如何添加默认值为迁移 0004,因此不会引发此异常。

不需要运行迁移,因为数据库是空的。

但是,south_migrationhistory 必须包含所有迁移及其应用时间的列表。

我尝试破解它并手动将迁移 0009 添加到数据库中,但这引发了另一个错误,因为中间迁移尚未运行。 我还尝试向数据库添加一个字段,看看是否可以弄清楚 add_column 的语法看起来像提供的默认值 0 一样。 该格式看起来与这些旧迁移完全不同。

以下是 add_column 语法的 2 个不同版本:

#0004 syntax:  
db.add_column('experiments_dailyreport', 'test_group_size', orm['experiments.dailyreport:test_group_size'])
#0009 syntax:
syntax db.add_column('experiments_dailyreport', 'test_group_size', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False)

因此,我猜测从创建 0004 到今天,南方代码发生了变化。

有没有办法使用syncdb构建数据库,然后以某种方式更新south_migrationhistory而不运行manage.py migrate?

如果您有一个带有South迁移的现有应用程序,您将如何从头开始构建一个新数据库?

我无法迁移,因为整数字段没有设置默认值。 如何在较旧的迁移中设置默认值?该领域甚至不再存在。

尝试的语法:

db.add_column('experiments_dailyreport', 'test_group_size', orm['experiments.dailyreport:test_group_size'], {'default': 0})
#throws ValueError: You cannot add a null=False column without a default value.
db.add_column('experiments_dailyreport', 'test_group_size', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False)
#throws AttributeError: Migration instance has no attribute 'gf'

我正在使用 South-0.7.2-py2.7.egg

I'm working with an OS project that uses South to migrate the database.
I'm building a new database from scratch and I want to make sure South is setup so I can easily update the database in the future.

It seems this process should be:

  1. create the db
  2. syncdb
  3. migrate

However, when I try to migrate the databases, one of the the earlier migrations(migration 0004 and they go to 0009) throws an exception:

ValueError: You cannot add a null=False column without a default value.

I don't understand how to add a default value to migration 0004 so this exception isn't thrown.

The migrations don't need to be run through because the database is empty.

However, south_migrationhistory must contain a list of all migrations and when they were applied.

I tried to hack it and just add migration 0009 to the database manually, but this threw another error because the intermediary migrations had not been run.
I also tried to add a field to database to see if I could figure out the syntax of add_column would look like with a default value of 0 supplied.
The format looks totally different than these older migrations.

Here are the 2 different versions of the add_column syntax:

#0004 syntax:  
db.add_column('experiments_dailyreport', 'test_group_size', orm['experiments.dailyreport:test_group_size'])
#0009 syntax:
syntax db.add_column('experiments_dailyreport', 'test_group_size', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False)

So, I'm guessing there was a change in the South code between when 0004 was created and today.

Is there a way to build the database with syncdb and then somehow update south_migrationhistory without running manage.py migrate ?

If you have an existing app with South migrations, how would you build a new database from scratch?

I can't migrate because there is no default set on a integer field.
How do I set a default in an older migration? The field doesn't even exist anymore.

syntaxes attempted:

db.add_column('experiments_dailyreport', 'test_group_size', orm['experiments.dailyreport:test_group_size'], {'default': 0})
#throws ValueError: You cannot add a null=False column without a default value.
db.add_column('experiments_dailyreport', 'test_group_size', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False)
#throws AttributeError: Migration instance has no attribute 'gf'

I'm using South-0.7.2-py2.7.egg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里梦着梦中梦 2024-10-08 14:39:41

Andrew Godwin 提供了答案:

是的,使用:

./manage.py syncdb --all  

then:

./manage.py migrate --fake  

但是,这也会忽略向数据库添加数据的任何迁移。

Andrew Godwin provided an answer:

Yes, use:

./manage.py syncdb --all  

then:

./manage.py migrate --fake  

This will, however, also ignore any migrations that add in data to the database.

蔚蓝源自深海 2024-10-08 14:39:41

不再支持 syncdb,请参阅此 答案

  1. 删除所有名为“migrations”的文件夹。
  2. 转到终端并运行:
  • ./manage.py makemigrations
  • ./manage.py migrate --run-syncdb

syncdb is no longer supported, instead, see this answer:

  1. Delete all the folders named 'migrations'.
  2. Go to terminal and run:
  • ./manage.py makemigrations
  • ./manage.py migrate --run-syncdb
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文