想要添加一列作为现有表的外键
在我的 Django 应用程序 models.py 中,我最近添加了这个字段。
Class Client
user = models.ForeignKey(User)
我想将mysql中的这个字段添加到现有表中。问题是我不知道处理外键的命令。我想要添加此列的表的名称称为 tiptop_clients
。
In my Django app, models.py, I have this field that I have just recently added.
Class Client
user = models.ForeignKey(User)
I want to add this field in mysql to an existing table. Problem is I don't know the command for dealing with foreign keys. The name of my table that I want to add this column is called tiptop_clients
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
sqldiff
-命令 django-extensions:You could use the
sqldiff
-command of django-extensions:您将遇到的第一个问题是如何处理现有数据?对于已创建的任何客户端,用户是否应该为 NULL?您会更新所有表格来处理这种情况吗?
您可以查看 South 项目来处理添加该列,但是你可能想看看这个相关的Stack Overflow有人想要的问题添加用户使用现有模型。
最后,他们认为从头开始比硬塞外键更容易。
The number one problem you are going to have with this is what do you do with the existing data? Should the user be NULL for any Client already created? Will you update all of your forms to handle that case?
You can take a look at the South project to handle adding that column, but you may want to look at this related Stack Overflow question where someone wanted to add User to an existing model.
In the end they decided it was easier to just start from scratch then to shoehorn the ForeignKey in.