codes
対応するニーモニックを含む数値のリストがありnames
、それらの Django モデルが必要なので、それらは主キーですが、列names
の値が一意であるという制約もあります。code
私が試したことは次のとおりです。
class Constant(models.Model):
name = models.CharField(max_length=70)
name.primary_key = True
code = models.IntegerField()
description = models.CharField(max_length=100)
unique_together = (("code",),)
一連の列で値の一意性を強制することを意図していることは理解していunique_together
ますが、1つだけで試してみるとうまくいくように見えましたpython manage.py syncdb
。
mysql> describe constant;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| name | varchar(70) | NO | PRI | | |
| code | int(11) | NO | | | |
| description | varchar(100) | NO | | | |
+-------------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> insert into constant values ('x',1,'fooo');
Query OK, 1 row affected (0.00 sec)
mysql> insert into constant values ('y',1,'foooo');
Query OK, 1 row affected (0.00 sec)
両方の列の値が一意であることを確認するにはどうすればよいですか?