省流助手:
ps:首先你需要知道
python manage.py makemigrations 在mirgrtions文件夹下生成更新文件
python manage.py migrate Formal --fake 跳过执行步骤按照更新文件生成更新日志(在数据库的django_migrations表内)
python manage.py mirgrate (检查数据库日志,如果存在该条日志,则会返回无最新更新,否则)按照更新文件更新数据库
1.用#注销你未更新的那一段代码
2.删除你mirgrtions文件夹下除__init__.py文件的其他文件
3.用数据可视化工具,进入数据库将django_migrations表内与你的app名一样的数据删掉
4.执行python manage.py makemigrations
5.python manage.py migrate Formal --fake
6.把注销代码恢复,再次执行python manage.py makemigrations
7.执行python manage.py migrate
正文:
今天在写项目的时候需要创建两个外键
刚开始我是这么写的
class PbWordModel(models.Model):id = models.AutoField(primary_key=True)u_id = models.ForeignKey(UsersModel, related_name='u_id', on_delete=models.CASCADE)word = models.CharField(max_length=255, null=False)class CzWordModel(models.Model):id = models.AutoField(primary_key=True)u_id = models.ForeignKey(UsersModel, related_name='u_id', on_delete=models.CASCADE)word = models.CharField(max_length=255, null=False)
之后我就python manage.py mirgrate
报:
(base) D:\pycharm cx\Test\class\BigHomeworkDjango>python manage.py migrate
SystemCheckError: System check identified some issues:ERRORS:
Formal.CzWordModel.u_id: (fields.E302) Reverse accessor for 'Formal.CzWordModel.u_id' clashes with field name 'Formal.UsersModel.u_id'.HINT: Rename field 'Formal.UsersModel.u_id', or add/change a related_name argument to the definition for field 'Formal.CzWordModel.u_id'.
Formal.CzWordModel.u_id: (fields.E303) Reverse query name for 'Formal.CzWordModel.u_id' clashes with field name 'Formal.UsersModel.u_id'.HINT: Rename field 'Formal.UsersModel.u_id', or add/change a related_name argument to the definition for field 'Formal.CzWordModel.u_id'.
Formal.CzWordModel.u_id: (fields.E304) Reverse accessor for 'Formal.CzWordModel.u_id' clashes with reverse accessor for 'Formal.PbWordModel.u_id'.HINT: Add or change a related_name argument to the definition for 'Formal.CzWordModel.u_id' or 'Formal.PbWordModel.u_id'.
Formal.CzWordModel.u_id: (fields.E305) Reverse query name for 'Formal.CzWordModel.u_id' clashes with reverse query name for 'Formal.PbWordModel.u_id'.HINT: Add or change a related_name argument to the definition for 'Formal.CzWordModel.u_id' or 'Formal.PbWordModel.u_id'.
Formal.PbWordModel.u_id: (fields.E302) Reverse accessor for 'Formal.PbWordModel.u_id' clashes with field name 'Formal.UsersModel.u_id'.HINT: Rename field 'Formal.UsersModel.u_id', or add/change a related_name argument to the definition for field 'Formal.PbWordModel.u_id'.
Formal.PbWordModel.u_id: (fields.E303) Reverse query name for 'Formal.PbWordModel.u_id' clashes with field name 'Formal.UsersModel.u_id'.HINT: Rename field 'Formal.UsersModel.u_id', or add/change a related_name argument to the definition for field 'Formal.PbWordModel.u_id'.
Formal.PbWordModel.u_id: (fields.E304) Reverse accessor for 'Formal.PbWordModel.u_id' clashes with reverse accessor for 'Formal.CzWordModel.u_id'.HINT: Add or change a related_name argument to the definition for 'Formal.PbWordModel.u_id' or 'Formal.CzWordModel.u_id'.
Formal.PbWordModel.u_id: (fields.E305) Reverse query name for 'Formal.PbWordModel.u_id' clashes with reverse query name for 'Formal.CzWordModel.u_id'.HINT: Add or change a related_name argument to the definition for 'Formal.PbWordModel.u_id' or 'Formal.CzWordModel.u_id'.
修改:related_name='u_id' 这个字段是唯一字段,改成不一样的名儿就行了
之后我又执行了
又报:
(base) D:\pycharm cx\Test\class\BigHomeworkDjango>python manage.py migrate
Operations to perform:Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:No migrations to apply.Your models in app(s): 'Formal' have changes that are not yet reflected in a migration, and so won't be applied.Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
这个也简单,这是意思就是你得先运行一下python manage.py makemigrations
也改好啦,我再运行python manage.py mirgrate
又报:
django.db.utils.OperationalError: (1050, "Table 'formal_newsmodel' already exists")
这回,我直接上网上查去了,只能查到一个结果
发现都是一篇博文来回来去的转载,抄袭,这是最早的一篇(2017.6.7)django.db.utils.OperationalError: 1050解决方案 - 村里汉子 - 博客园 (cnblogs.com))
那行,那我就执行这个呗
python manage.py migrate Formal --fake
执行完,噩梦就开始了
是,不报错了,表也没更新
我人傻了,重新执行几次,结果都是
Operations to perform:Apply all migrations: Formal, admin, auth, contenttypes, sessions
Running migrations:No migrations to apply.
意思就是,你的表是最新的,不用更新
后来,疯狂百度,终于查到了最总要的信息!
Django 输入python manage.py migrate不能新建表_追-CSDN博客
django更新数据库的时候
会将更新的这一条加到更新日志中
(ps:name是mirgrtions文件夹内更新文件的名字)
同时,这个日志也是更新数据库的关键
你需要删掉这个日志它才会更新,否则它会认为这次他已经更新过了
python manage.py migrate Formal --fake这句话的作用是:生成一个假的日志
为什么是说它是假日志呢?因为django根本没有更新库,他只是加入了这样一条日志而已,所以好几次执行的结果都是No migrations to apply.但是库并没有更新
所以我就删除了mirgrtions内的更新文件,将我新加的代码注销掉
用python manage.py makemigrations生成更新日志,因为这些库都已经存在了,所以你现在执行python manage.py migrate一定会出错,告诉你 库已经存在了,所以现在就需要执行python manage.py migrate Formal --fake生成一个假日志,跳过执行这个步骤
此时在恢复注销的代码
重新运行
python manage.py makemigrations
python manage.py migrate
就好啦!
如有转载,请注明原文地址
否则我会将根据《中华人民共和国著作权法》对抄袭人提起诉讼