KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to add a ForeignKey field to my model. In order to achieve that, I did 3 steps: Added the ForeignKey field with null=True to my model, and then created a schemamigration . Created a datamigration in order to fill that foreign key with a sensible value. After this migration is applied, I'm pretty sure there is no object left with a NULL value at that field. Changed that ForeignKey field to null=False , and then created another schemamigration . However, I'm having problems at the step 3 : ? The field 'MyModel.fkfield' does not have a default specified, yet is NOT NULL. ? Since you are making this field non-nullable, you MUST specify a default ? value to use for existing rows. Would you like to: ? 1. Quit now, and add a default to the field in models.py ? 2. Specify a one-off value to use for existing columns now ? Please select a choice: Well... No default value makes sense for this field, and also by now I'm pretty sure no object has that field as NULL. So... I don't know how to answer this question correctly. If I could, I would say "don't set any default value, and throw an error if you find any NULL in that column". For now, as a workaround just to get over that question, I'm choosing option 2 and then using 1 as the one-off default value. This is a quite bad solution, but at least south doesn't complain about it and creates the migration. What is the correct way of changing a field into NOT NULL ? (using south ) What should I do when south asks me for a default value for a field that shouldn't have one? I'm using django-1.2 and south-0.7 . Related question: <a href="https://stackov
Tags (comma-separated)
Save Edits
Cancel