简体   繁体   English

Django / MySql不尊重unique_together

[英]Django / MySql not honouring unique_together

I am using django with mysql (InnoDB) and have the following in my django model: 我将django与mysql(InnoDB)结合使用,并且在django模型中具有以下内容:

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 : 运行python manage.py sql app_name给出了:

However within mysql client doing desc row_locks gives: 但是在mysql客户端中做desc row_locks会给出:

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. 还检查了我是否可以为table_name和locked_row_id输入具有相同值的重复行,而不会出现完整性错误。

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, 现在我的假设是我在这里做错了,因为这样一个显而易见的事情不可能像个bug一样在野外,但是我看不到,

Any fresh eyes would be appreciated 任何新鲜的眼睛将不胜感激

Rob

Update: So as Dominic pointed out the problem was the south migration not creating the unique constraint. 更新:正如多米尼克(Dominic)所指出的那样,问题在于南迁徙并未产生独特的约束。 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. 我本来可以进行两次迁移,一次迁移创建表,然后一次迁移添加unique_together-不知道那是否行得通-可能会尝试花费更多时间。

In any case I got around it by manually editing the forward method in the south migration script as follows: 无论如何,我都可以通过如下方式手动编辑南迁移脚本中的forward方法来解决它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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