Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL中使用涉及ForeignKey字段的唯一约束?_Mysql_Django_Django South - Fatal编程技术网

如何在MySQL中使用涉及ForeignKey字段的唯一约束?

如何在MySQL中使用涉及ForeignKey字段的唯一约束?,mysql,django,django-south,Mysql,Django,Django South,MySQL在使用外键的多列唯一约束方面似乎有点崩溃。下面是我能给出的最小示例(使用MySQL/InnoDB): models.py from django.db import models class Team(models.Model): pass class Player(models.Model): team = models.ForeignKey(Team) number = models.PositiveIntegerField() class M

MySQL在使用外键的多列唯一约束方面似乎有点崩溃。下面是我能给出的最小示例(使用MySQL/InnoDB):

models.py

from django.db import models

class Team(models.Model):
    pass

class Player(models.Model):
    team = models.ForeignKey(Team)
    number = models.PositiveIntegerField()

    class Meta:
        unique_together = ("team", "number")
运行
schemamigration--initial
,south抛出以下迁移(仅重要位):

在MySQL中:

mysql> SHOW COLUMNS FROM fkuniq_player;
+---------+------------------+------+-----+---------+----------------+
| Field   | Type             | Null | Key | Default | Extra          |
+---------+------------------+------+-----+---------+----------------+
| id      | int(11)          | NO   | PRI | NULL    | auto_increment |
| team_id | int(11)          | NO   | MUL | NULL    |                |
| number  | int(10) unsigned | NO   |     | NULL    |                |
+---------+------------------+------+-----+---------+----------------+
我认为南方没有创造出我想要的独特约束。在
Key
列中,我看到
id
上的主键索引和
team\u id
上的外键索引,但是
number
行中也应该有一个
MUL
,因为它上面应该有一个
UNIQUE
索引和
team\u id
。此外,从模型中同时删除
unique_
约束会导致下一次迁移失败,并出现以下错误:

Traceback (most recent call last):
  ...
  File "/home/aogier/uniques/../uniques/fkuniq/migrations/0002_auto__del_unique_player_number_team.py", line 12, in forwards
    db.delete_unique('fkuniq_player', ['number', 'team_id'])
  File "/home/aogier/.virtualenvs/uniques/lib/python2.7/site-packages/south/db/generic.py", line 479, in delete_unique
    raise ValueError("Cannot find a UNIQUE constraint on table %s, columns %r" % (table_name, columns))
ValueError: Cannot find a UNIQUE constraint on table fkuniq_player, columns ['number', 'team_id']
我认为它是缺失的,因为当外键约束和多列
UNIQUE
约束重合时,MySQL不能很好地发挥作用。关于
altertable
的MySQL文档中有这样一条注释(请参阅大约一半,Hadi Rastgou的注释)


不管怎么说,很抱歉问了这么长的问题:有人有办法让这项工作成功吗?我希望在迁移中有一种干净的方法来实现这一点,即使我必须用原始SQL编写特定于MySQL的查询。或者,这在MySQL中是完全不可能做到的,在我花更多时间做这件事之前,最好先知道这一点。

Argh,我自己解决了这个问题。这是南方的一个已知错误,在他们的开发分支中修复

Traceback (most recent call last):
  ...
  File "/home/aogier/uniques/../uniques/fkuniq/migrations/0002_auto__del_unique_player_number_team.py", line 12, in forwards
    db.delete_unique('fkuniq_player', ['number', 'team_id'])
  File "/home/aogier/.virtualenvs/uniques/lib/python2.7/site-packages/south/db/generic.py", line 479, in delete_unique
    raise ValueError("Cannot find a UNIQUE constraint on table %s, columns %r" % (table_name, columns))
ValueError: Cannot find a UNIQUE constraint on table fkuniq_player, columns ['number', 'team_id']