Django South - 表已经存在

发布于 2024-09-06 16:44:09 字数 477 浏览 7 评论 0原文

我正在尝试从南方开始。我有一个现有的数据库,并添加了 South(syncdbschemamigration --initial)。

然后,我更新了 models.py 以添加一个字段并运行 ./manage.py schemamigration myapp --auto。它似乎找到了该字段,并说我可以使用 ./manage.py migrate myapp 来应用它。但是,这样做会出现错误:

django.db.utils.DatabaseError: table "myapp_tablename" already exists

tablenamemodels.py 中列出的第一个表。

我正在运行 Django 1.2,South 0.7

I am trying to get started with South. I had an existing database and I added South (syncdb, schemamigration --initial).

Then, I updated models.py to add a field and ran ./manage.py schemamigration myapp --auto. It seemed to find the field and said I could apply this with ./manage.py migrate myapp. But, doing that gave the error:

django.db.utils.DatabaseError: table "myapp_tablename" already exists

tablename is the first table listed in models.py.

I am running Django 1.2, South 0.7

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

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

发布评论

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

评论(8

雨后咖啡店 2024-09-13 16:44:09

由于您已经在数据库中创建了表,因此您只需以假的方式运行初始迁移,

./manage.py migrate myapp --fake

确保模型的架构与数据库中表的架构相同。

since you already have the tables created in the database, you just need to run the initial migration as fake

./manage.py migrate myapp --fake

make sure that the schema of models is same as schema of tables in database.

戒ㄋ 2024-09-13 16:44:09

虽然表“myapp_tablename”已经存在,但错误停止引发
在我执行 ./manage.py migrate myapp --fake 之后,DatabaseError 显示
没有这样的列:myapp_mymodel.added_field。

遇到完全相同的问题!

1.首先检查导致此问题的迁移号。假设它是:0010。

2.您需要:

./manage.py schemamigration myapp --add-field MyModel.added_field
./manage.py migrate myapp

如果缺少多个字段,您必须对每个字段重复该操作。

3.现在您有了一堆新的迁移,因此从 myapp/migrations 中删除它们的文件(如果您需要添加多个字段,则为 0011 或更多)。

4.运行此命令:

./manage.py migrate myapp 0010

现在尝试 ./manage.py migrate myapp

如果它没有失败,那么您就准备好了。只需仔细检查是否缺少任何字段。

编辑:

当您有一个安装 South 的生产数据库并且在其他环境中创建的第一个初始迁移重复您数据库中已有的内容时,也可能会出现此问题。这里的解决方案更简单:

  1. 伪造第一次迁移:

    ./manage migrate myapp 0001 --fake

  2. 滚动其余迁移:

    ./manage migrate myapp

Although the table "myapp_tablename" already exists error stop raising
after I did ./manage.py migrate myapp --fake, the DatabaseError shows
no such column: myapp_mymodel.added_field.

Got exactly the same problem!

1.Firstly check the migration number which is causing this. Lets assume it is: 0010.

2.You need to:

./manage.py schemamigration myapp --add-field MyModel.added_field
./manage.py migrate myapp

if there is more than one field missing you have to repeat it for each field.

3.Now you land with a bunch of new migrations so remove their files from myapp/migrations (0011 and further if you needed to add multiple fields).

4.Run this:

./manage.py migrate myapp 0010

Now try ./manage.py migrate myapp

If it doesn't fail you're ready. Just doublecheck if any field's aren't missing.

EDIT:

This problem can also occur when you have a production database for which you install South and the first, initial migration created in other enviorment duplicates what you already have in your db. The solution is much easier here:

  1. Fake the first migration:

    ./manage migrate myapp 0001 --fake

  2. Roll with the rest of migrations:

    ./manage migrate myapp

肤浅与狂妄 2024-09-13 16:44:09

当我遇到这个错误时,它有不同的原因。

就我而言,South 以某种方式在我的数据库中留下了一个临时空表,该表用于 _remake_table()。也许我以一种不应该的方式中止了迁移。无论如何,每个后续的新迁移在调用 _remake_table() 时都会抛出错误 sqlite3.pypysqlite2.dbapi2.OperationalError: table "_south_new_myapp_mymodel"已经存在,因为它确实 已经存在并且不应该存在。

_south_new 位对我来说看起来很奇怪,所以我浏览了我的数据库,看到了表 _south_new_myapp_mymodel,挠了挠头,查看了 South 的源,认为它是垃圾,删除了表,一切都很好。

When I ran into this error, it had a different cause.

In my case South had somehow left in my DB a temporary empty table, which is used in _remake_table(). Probably I had aborted a migration in a way I shouldn't have. In any case, each subsequent new migration, when it called _remake_table(), was throwing the error sqlite3.pypysqlite2.dbapi2.OperationalError: table "_south_new_myapp_mymodel" already exists, because it did already exist and wasn't supposed to be there.

The _south_new bit looked odd to me, so I browsed my DB, saw the table _south_new_myapp_mymodel, scratched my head, looked at South's source, decided it was junk, dropped the table, and all was well.

ぺ禁宫浮华殁 2024-09-13 16:44:09

如果您遇到模型与数据库不匹配的问题,例如 @pielgrzym,并且您希望自动迁移数据库以匹配最新的 models.py 文件(并删除在迁移期间不会由装置重新创建的任何数据) ):

manage.py schemamigration myapp --initial
manage.py migrate myapp --fake
manage.py migrate myapp zero
manage.py migrate myapp

这只会删除并重新创建最新 models.py 文件中存在的数据库表,因此您的数据库中可能存在以前 syncdb 中的垃圾表或迁移。要摆脱这些,请在所有这些迁移之前执行以下操作:

manage.py sqlclear myapp | manage.py sqlshell

如果数据库中仍然存在一些问题,那么您必须执行 inspectdb 并创建 models.py< /code> 文件(对于您要清除的表和应用程序),然后执行 sqlclear ,然后在创建 --initial 之前恢复原始 models.py迁移并迁移到它。所有这些都是为了避免弄乱数据库所需的特定 SQL 风格。

If you have problems with your models not matching your database, like @pielgrzym, and you want to automatically migrate the database to match the latest models.py file (and erase any data that won't be recreated by fixtures during migrate):

manage.py schemamigration myapp --initial
manage.py migrate myapp --fake
manage.py migrate myapp zero
manage.py migrate myapp

This will only delete and recreate database tables that exist in your latest models.py file, so you may have garbage tables in your database from previous syncdbs or migrates. To get rid of those, precede all these migrations with:

manage.py sqlclear myapp | manage.py sqlshell

And if that still leaves some CRUFT lying around in your database then you'll have to do an inspectdb and create the models.py file from that (for the tables and app that you want to clear) before doing the sqlclear and then restore your original models.py before creating the --initial migration and migrating to it. All this to avoid messing around with the particular flavor of SQL that your database needs.

暖阳 2024-09-13 16:44:09

按顺序执行这些步骤可能会对您有所帮助

1) python manage.py schemamigration apps.appname --initial

上述步骤默认创建迁移文件夹。

2) python manage.py migrate apps.appname --fake

生成假迁移。

3) python manage.py schemamigration apps.appname --auto

然后您可以根据需要添加字段并执行上述命令。

4) python manage.py 迁移 apps.appname

Perform these steps in order may help you:

1) python manage.py schemamigration apps.appname --initial

Above step creates migration folder as default.

2) python manage.py migrate apps.appname --fake

generates a fake migration.

3) python manage.py schemamigration apps.appname --auto

Then you can add fields as you wish and perform the above command.

4) python manage.py migrate apps.appname

苍白女子 2024-09-13 16:44:09

如果您有现有的数据库和应用程序,则可以使用 South 转换命令。

./manage.py convert_to_south myapp

必须在对数据库中已有内容进行任何更改之前应用此命令。

Convert_to_south 命令仅在运行该命令的第一台机器上完全有效。一旦您将其初始迁移提交到您的 VCS 中,您就必须在每台拥有代码库副本的计算机上运行 ./manage.py migrate myapp 0001 --fake (make确保他们首先了解最新的模型和架​​构)。
参考:http://south.readthedocs.org/en/latest/convertinganapp.html

If you have an existing database and app you can use the south conversion command

./manage.py convert_to_south myapp

This has to be applied before you do any changes to what is already in the database.

The convert_to_south command only works entirely on the first machine you run it on. Once you’ve committed the initial migrations it made into your VCS, you’ll have to run ./manage.py migrate myapp 0001 --fake on every machine that has a copy of the codebase (make sure they were up-to-date with models and schema first).
ref: http://south.readthedocs.org/en/latest/convertinganapp.html

离旧人 2024-09-13 16:44:09

作为临时解决方案,您可以在迁移脚本中注释表创建。

class Migration(migrations.Migration):

    dependencies = [
        (...)
    ]

    operations = [
        #migrations.CreateModel(
        #    name='TABLE',
        #    fields=[
        #            ....
        #            ....
        #    ],
        #),
        ....
        ....

或者

如果现有表不包含行(空),则考虑删除该表,如下所示。 (仅当表不包含行时才建议执行此修复)。还要确保此操作在 createModel 操作之前。

class Migration(migrations.Migration):

    dependencies = [
        (...),
    ]

    operations = [
        migrations.RunSQL("DROP TABLE myapp_tablename;")
    ]

As temporary solution, you can comment the Table creation in the migration script.

class Migration(migrations.Migration):

    dependencies = [
        (...)
    ]

    operations = [
        #migrations.CreateModel(
        #    name='TABLE',
        #    fields=[
        #            ....
        #            ....
        #    ],
        #),
        ....
        ....

Or

If the existing table contains no rows (empty), then consider deleting the table like below. (This fix is recommended only if the table contains no rows). Also make sure this operation before the createModel operation.

class Migration(migrations.Migration):

    dependencies = [
        (...),
    ]

    operations = [
        migrations.RunSQL("DROP TABLE myapp_tablename;")
    ]
你好,陌生人 2024-09-13 16:44:09

还有一个解决方案(也许是临时解决方案)。

$ python manage.py sqlmigrate APP_NAME MIGRATION_NAME

例如。,。

$ python manage.py sqlmigrate users 0029_auto_20170310_1117

这将列出原始 SQL 查询中的所有迁移。您可以选择要运行的查询,避免创建现有表的部分

One more solution(maybe a temporary solution).

$ python manage.py sqlmigrate APP_NAME MIGRATION_NAME

eg.,.

$ python manage.py sqlmigrate users 0029_auto_20170310_1117

This will list all the migrations in raw sql queries. You can pick the queries which you want to run avoiding the part which creates the existing table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文