Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - legacy db and problems with 'id' field
    primarykey
    data
    text
    <p>I'm integrating a MySQL database from a php app into a new Django project. <a href="https://docs.djangoproject.com/en/1.3/ref/django-admin/#django-admin-inspectdb" rel="nofollow">Inspectdb</a> 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.</p> <p>The problem is when I try to create a new entry, I get the error <strong><code>"Field 'id' doesn't have a default value"</code></strong>. The traceback starts from the <code>form.save()</code> call and the exception is coming from the MySQL cursor. In most cases the column is named <code>id</code> but in one case it is a named value:</p> <pre><code>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 = ... </code></pre> <p>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 <code>id</code> values in the shell, I can see that they are incrementing correctly.</p> <p>But for ModelOne (with just <code>id</code>), 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)</p> <p>What do I need to do to get these id fields working correctly?</p> <hr> <p>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.</p> <hr> <p>update2: Info from db shell</p> <p>ModelOne:</p> <pre><code>+-------------+--------------+-------+------+---------+-----------------+ | Field | Type | Null | Key | Default | Extra | | id | int(11) | NO | PRI | NULL | auto_increment | </code></pre> <p>ModelTwo:</p> <pre><code>+-------------+--------------+-------+------+---------+-----------------+ | Field | Type | Null | Key | Default | Extra | | named_pk | int(11) | NO | PRI | NULL | auto_increment | </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload