[英]How can I solve an error when I migrate my Django models?
I want to migrate my model for create tables on my local PgSQL DB.我想迁移我的模型以在本地 PgSQL DB 上创建表。 It's not the first time that I'm doing that on this configuration but this time, that's fail.
这不是我第一次在这个配置上这样做,但这次失败了。
1) I deleted my old database jlb_inventory 1)我删除了我的旧数据库jlb_inventory
2) I recreated the database jlb_inventory empty 2)我重新创建了数据库jlb_inventory空
3) I deleted "0001_initial" on application's directory /migration
3)我删除了应用程序目录
/migration
“0001_initial”
4) I ran command python manage.py makemigrations
=> Error 4)我运行命令
python manage.py makemigrations
=>错误
models.py模型.py
# Table Etude
class Study(models.Model):
study_name = models.CharField(max_length=255, null=False)
class Inventory(models.Model):
watercourse = models.CharField(max_length=255, null=False)
town = models.CharField(max_length=255, null=False)
number_resorts = models.IntegerField(null=False)
inventory_date = models.DateField(null=False)
fk_study = models.ForeignKey(Study, on_delete=models.CASCADE)
class Resort(models.Model):
index_resort = models.IntegerField(null=False)
name_resort = models.CharField(max_length=255)
fk_inventory = models.ForeignKey(Inventory, on_delete=models.CASCADE)
class Taxon(models.Model):
name_taxon = models.CharField(max_length=255, null=False)
gi = models.IntegerField(default=0)
class Sample(models.Model):
MICRO_HABITAT_CHOICES = (
('1', 'Habitat1'),
('2', 'Habitat2'),
('3', 'Habitat3'),
('4', 'Habitat4'),
('5', 'Habitat5'),
('6', 'Habitat6'),
('7', 'Habitat7'),
('8', 'Habitat8'),
)
taxon_quantity = models.IntegerField(null=False)
fk_taxon = models.ForeignKey(Taxon, on_delete=models.CASCADE)
fk_resort = models.ForeignKey(Resort, on_delete=models.CASCADE)
And my error而我的错误
django.db.utils.ProgrammingError: relation "business_data_entry_taxon" does not exist
LINE 1: ...ame_taxon", "business_data_entry_taxon"."gi" FROM "business_...
Someone knows what is the problem here ?有人知道这里有什么问题吗? Thanks!
谢谢!
I tested your app and it is working(django 2.2).我测试了你的应用程序,它正在运行(django 2.2)。 If you delete db and recreate new db so normally it is working but somehow I think you need first migration for your db.
如果您删除数据库并重新创建新数据库,那么它通常可以正常工作,但我认为您需要先迁移数据库。
So, remove app INSTALLED_APPS, and recreate db and remove all migrations.因此,删除应用程序 INSTALLED_APPS,然后重新创建 db 并删除所有迁移。 Then run makemigrations and migrate.
然后运行 makemigrations 并迁移。 Then add your app to INSTALLED_APPS, run again makemigrations and migrate.
然后将您的应用程序添加到 INSTALLED_APPS,再次运行 makemigrations 并迁移。
I post an aswer but I don't have a structuration explication about the solution.我发布了一个答案,但我没有关于解决方案的结构说明。
In my app, I've 4 forms and 2 call the DB for ChoiceField
.在我的应用程序中,我有 4 个表单,2 个为
ChoiceField
调用数据库。 I tried to comment all my path
in urls.py
and ran command makemigrations
and migrate
.我试图在
urls.py
评论我的所有path
并运行命令makemigrations
和migrate
。 That's work ...这就是工作...
I think, before execute the command, Django "compile" (or something like that) my code and try to set ChoiceField but he can't find something because I deleted the DB before.我认为,在执行命令之前,Django“编译”(或类似的东西)我的代码并尝试设置 ChoiceField 但他找不到某些东西,因为我之前删除了数据库。
I encourage someone to comment this post for more explications then, i'll edit this aswer.我鼓励有人评论这篇文章以获得更多解释,然后我会编辑这个 aswer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.