Django South - 表已经存在
我正在尝试从南方开始。我有一个现有的数据库,并添加了 South(syncdb
、schemamigration --initial
)。
然后,我更新了 models.py
以添加一个字段并运行 ./manage.py schemamigration myapp --auto
。它似乎找到了该字段,并说我可以使用 ./manage.py migrate myapp
来应用它。但是,这样做会出现错误:
django.db.utils.DatabaseError: table "myapp_tablename" already exists
tablename
是 models.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
由于您已经在数据库中创建了表,因此您只需以假的方式运行初始迁移,
确保模型的架构与数据库中表的架构相同。
since you already have the tables created in the database, you just need to run the initial migration as fake
make sure that the schema of models is same as schema of tables in database.
遇到完全相同的问题!
1.首先检查导致此问题的迁移号。假设它是:0010。
2.您需要:
如果缺少多个字段,您必须对每个字段重复该操作。
3.现在您有了一堆新的迁移,因此从 myapp/migrations 中删除它们的文件(如果您需要添加多个字段,则为 0011 或更多)。
4.运行此命令:
现在尝试 ./manage.py migrate myapp
如果它没有失败,那么您就准备好了。只需仔细检查是否缺少任何字段。
编辑:
当您有一个安装 South 的生产数据库并且在其他环境中创建的第一个初始迁移重复您数据库中已有的内容时,也可能会出现此问题。这里的解决方案更简单:
伪造第一次迁移:
./manage migrate myapp 0001 --fake
滚动其余迁移:
./manage migrate myapp
Got exactly the same problem!
1.Firstly check the migration number which is causing this. Lets assume it is: 0010.
2.You need to:
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:
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:
Fake the first migration:
./manage migrate myapp 0001 --fake
Roll with the rest of migrations:
./manage migrate myapp
当我遇到这个错误时,它有不同的原因。
就我而言,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.如果您遇到模型与数据库不匹配的问题,例如 @pielgrzym,并且您希望自动迁移数据库以匹配最新的 models.py 文件(并删除在迁移期间不会由装置重新创建的任何数据) ):
这只会删除并重新创建最新
models.py
文件中存在的数据库表,因此您的数据库中可能存在以前syncdb
中的垃圾表或迁移
。要摆脱这些,请在所有这些迁移之前执行以下操作:如果数据库中仍然存在一些问题,那么您必须执行
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
):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 previoussyncdb
s ormigrate
s. To get rid of those, precede all these migrations with:And if that still leaves some CRUFT lying around in your database then you'll have to do an
inspectdb
and create themodels.py
file from that (for the tables and app that you want to clear) before doing thesqlclear
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.按顺序执行这些步骤可能会对您有所帮助
: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
如果您有现有的数据库和应用程序,则可以使用 South 转换命令。
必须在对数据库中已有内容进行任何更改之前应用此命令。
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
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
作为临时解决方案,您可以在迁移脚本中注释表创建。
或者
如果现有表不包含行(空),则考虑删除该表,如下所示。 (仅当表不包含行时才建议执行此修复)。还要确保此操作在 createModel 操作之前。
As temporary solution, you can comment the Table creation in the migration script.
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.
还有一个解决方案(也许是临时解决方案)。
例如。,。
这将列出原始 SQL 查询中的所有迁移。您可以选择要运行的查询,避免创建现有表的部分
One more solution(maybe a temporary solution).
eg.,.
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