简体   繁体   English

迁移 Django 模型时如何解决错误?

[英]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.这不是我第一次在这个配置上这样做,但这次失败了。

My manipulation :我的操作:

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并运行命令makemigrationsmigrate 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.

相关问题 在django中迁移模型时如何解决SQLdecode错误? - How to solve SQLdecode error when you migrate models in django? 当我尝试执行 django 模型迁移命令时出现错误 - python manage.py 迁移。 如何解决这个错误? - I am getting an error in when i am trying to execute django models migrate command - python manage.py migrate . How to solve this error? 如何在不更改模型的情况下解决 django 中的这个问题? - How can I solve this issue in django without changing the models? Django 如何解决 NoReverseMatch 错误? - Django How can I solve NoReverseMatch error? 在Django中使用Migration命令时如何获取求解值错误 - How do i get solve value error while using migrate command in django 尝试迁移 Django 中的模型时出现值错误 - Value error when trying to migrate models in Django 当我在模板中显示数据时,如何解决 Django 中的价格错误? - How to solve price error in Django when I am displaying data in my template? 如何在我的 Django 模型中强制执行 inheritance? - How can I enforce inheritance in my Django models? 如何查看 django 中的 models.py? - How I can give a view to my models.py in django? 如何告诉Django在syncdb上包含我的模型的表? - How can I tell Django to include tables for my models on a syncdb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM