Django - legacy db and problems with 'id' field
I'm integrating a MySQL database from a php app into a new Django project. Inspectdb worked well, I just had to change a couple of fields to ForeignKeys and now all the reading and editing current data is working great.
The problem is when I try开发者_StackOverflow to create a new entry, I get the error "Field 'id' doesn't have a default value"
. The traceback starts from the form.save()
call and the exception is coming from the MySQL cursor. In most cases the column is named id
but in one case it is a named value:
class ModelOne(models.Model): #normal "id" named pk
id = models.AutoField(primary_key=True, db_column='id', default=None)
other_fields = ...
class ModelTwo(models.Model): #specific pk
named_pk = models.AutoField(primary_key=True, db_column='named_pk',
default=None)
other_fields = ...
For ModelTwo, when I POST a valid form, I get the error, but then if I go back to my data list, the new item shows up! And after I checked the latest id
values in the shell, I can see that they are incrementing correctly.
But for ModelOne (with just id
), the error still shows up, and the pk become 2147483647 (the max) and subsequent saves fail because of duplicate ids. (the next highest pk is only 62158)
What do I need to do to get these id fields working correctly?
update: Still no luck fixing this. Thinking about dumping the data and importing it into fresh, Django-built tables. Still looking for a solution to this problem.
update2: Info from db shell
ModelOne:
+-------------+--------------+-------+------+---------+-----------------+
| Field | Type | Null | Key | Default | Extra |
| id | int(11) | NO | PRI | NULL | auto_increment |
ModelTwo:
+-------------+--------------+-------+------+---------+-----------------+
| Field | Type | Null | Key | Default | Extra |
| named_pk | int(11) | NO | PRI | NULL | auto_increment |
I met the same issue after some complex South migration. We wanted to avoid to reload the database (dump/import), luckily it will help other peoples who fall on this post after searching for the same issue.
We found a solution which solve this problem without the need of exporting and importing the database.
For a table named auth_user, the following MySQL command will fixe the above error message:
ALTER TABLE auth_user MODIFY `id` INT(11) NOT NULL AUTO_INCREMENT;
original solution from :
http://webit.ca/2012/01/field-id-doesnt-have-a-default-value/
It makes sense to define pk for ModelTwo (as your are already doing) because your pk has different name 'named_pk'. However, no need to explicitly define 'id' as your pk for ModelOne. Django will create id column by default. So do not define the id column at all for ModelOne.
UPDATE: remove "default=None" from the model and default NULL from the database for ModelTwo for named_pk
I ended up exporting the data from the original database, and loading it into a fresh db which was generated by my models/project.
I think I had tried too many things with the copy of the original database I was working on and botched up the underlying SQL for the pk field.
I'm just glad that I was only working with a copy.
精彩评论