Want to add a column which is a Foreign key to an existing table
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开发者_C百科 know the command for dealing with foreign keys. The name of my table that I want to add this column is called tiptop_clients
.
You could use the sqldiff
-command of django-extensions:
Django command that scans all models for the given appnames and compares there database schema with the real database tables.
It indicates how columns in the database are different from the SQL that would be generated by Django. This command is not a database migration tool. Though it might certainly be of help during migrations. It’s purpose is to show the current differences as a way to checking or debugging your models compared to the real database tables and columns.
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.
精彩评论