简体   繁体   中英

Django / MySql not honouring unique_together

I am using django with mysql (InnoDB) and have the following in my django model:

class RowLock(models.Model):
    table_name = models.CharField(blank = False, max_length = 30)
    locked_row_id = models.IntegerField(null = False)
    process_id = models.IntegerField(null = True)
    thread_id = models.IntegerField(null = True)
    class Meta:
        db_table = "row_locks"
        unique_together = (("table_name", "locked_row_id"),)

Running python manage.py sql app_name gives :

However within mysql client doing desc row_locks gives:

mysql> desc row_locks;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| id            | int(11)     | NO   | PRI | NULL    | auto_increment |
| table_name    | varchar(30) | NO   |     | NULL    |                |
| locked_row_id | int(11)     | NO   |     | NULL    |                |
| process_id    | int(11)     | YES  |     | NULL    |                |
| thread_id     | int(11)     | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

Have also checked that I can enter duplicate rows with same values for table_name and locked_row_id without integrity error.

Now my assumption is that I am doing something wrong here because such an obvious thing could not be in the wild as a bug, but I can't see it,

Any fresh eyes would be appreciated

Rob

Update: So as Dominic pointed out the problem was the south migration not creating the unique constraint. I could have looked at doing 2 migrations, one to create the table and then a subsequent one to add the unique_together - don't know if that would have worked or not - may try with more time.

In any case I got around it by manually editing the forward method in the south migration script as follows:

As generated by south:

class Migration(SchemaMigration):

    def forwards(self, orm):

        # Adding model 'RowLock'                                                                                           
        db.create_table('row_locks', (
            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('table_name', self.gf('django.db.models.fields.CharField')(max_length=30)),
            ('locked_row_id', self.gf('django.db.models.fields.IntegerField')()),
            ('process_id', self.gf('django.db.models.fields.IntegerField')(null=True)),
            ('thread_id', self.gf('django.db.models.fields.IntegerField')(null=True)),
        ))
        db.send_create_signal('manager', ['RowLock'])

Manually edited:

class Migration(SchemaMigration):

    def forwards(self, orm):

        # Adding model 'RowLock'                                                                                           
        db.create_table('row_locks', (
            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('table_name', self.gf('django.db.models.fields.CharField')(max_length=30)),
            ('locked_row_id', self.gf('django.db.models.fields.IntegerField')()),
            ('process_id', self.gf('django.db.models.fields.IntegerField')(null=True)),
            ('thread_id', self.gf('django.db.models.fields.IntegerField')(null=True)),
        ))
        db.create_index('row_locks', ['table_name','locked_row_id'], unique=True)
        db.send_create_signal('manager', ['RowLock'])

For completeness , my last update should have probably been added as an answer.

I fixed the problem by manually editing the forward method of the south migration and added the line:

db.create_index('row_locks', ['table_name','locked_row_id'], unique=True)

Rob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM