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 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 |