Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.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
Python django unique在一起不适用于none datetime_Python_Django_Orm_Model - Fatal编程技术网

Python django unique在一起不适用于none datetime

Python django unique在一起不适用于none datetime,python,django,orm,model,Python,Django,Orm,Model,我正在使用django 1.5.5、python 2.7和MySQL 这是我的模型 class Foo(models.Model): user = models.ForeignKey(User) date = models.DateTimeField(null=True, blank=True,editable=True) class Meta: unique_together = ('user', 'date') 如果我使用此选项,我可以添加

我正在使用django 1.5.5、python 2.7和MySQL

这是我的模型

class Foo(models.Model):
     user = models.ForeignKey(User)
     date = models.DateTimeField(null=True, blank=True,editable=True)

     class Meta:
         unique_together = ('user', 'date')
如果我使用此选项,我可以添加两条具有相同用户和空日期的记录。
我想唯一性被强制执行,即使是空日期

表创建命令

CREATE TABLE `management_foo` 
(  `id` int(11) NOT NULL AUTO_INCREMENT,  
   `user_id` int(11) NOT NULL, 
   `date` datetime DEFAULT NULL,  
    PRIMARY KEY (`id`),  
    UNIQUE KEY `user_id` (`user_id`,`date`),  
    KEY `management_foo_6232c63c` (`user_id`),  
    CONSTRAINT `user_id_refs_id_d47e5747` FOREIGN KEY (`user_id`) 
    REFERENCES `auth_user` (`id`)) 
    ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
和表格描述

+-------------+--------------+------+-----+---------+----------------+  
| Field       | Type         | Null | Key | Default | Extra          |  
+-------------+--------------+------+-----+---------+----------------+  
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |   
| user_id     | int(11)      | NO   | MUL | NULL    |                |   
| date        | datetime     | YES  |     | NULL    |                |  
+-------------+--------------+------+-----+---------+----------------+   

在InnoDB中,每个NULL都被视为唯一值

例如:

mysql> create table x (i int, j int, primary key (i), unique key (j));
mysql> insert into x (i,j) values (NULL,NULL);
ERROR 1048 (23000): Column 'i' cannot be null
mysql> insert into x (i,j) values (1,NULL);
mysql> insert into x (i,j) values (2,NULL);
mysql> insert into x (i,j) values (3,3);
mysql> select * from x;
+---+------+
| i | j    |
+---+------+
| 1 | NULL |
| 2 | NULL |
| 3 |    3 |
+---+------+
3 rows in set (0.01 sec)

mysql> insert into x (i,j) values (4,3);
ERROR 1062 (23000): Duplicate entry '3' for key 'j'
您需要从字段定义中添加
notnull
约束删除
null=True
(例如用默认值替换)


顺便说一句,最好以这样的风格编写:
unique\u-together=((“field1”、“field2”),
–仅为了记录,扩展唯一对会容易得多:SQL标准声明
NULL
不是一个值,因此不能考虑
unique
约束。看起来它与Django无关,实际上是预期的行为


有关技术解决方案,请参见akaRem的回答。

此表在DB中的外观如何?@akaRem两条记录在数据库中的日期均为空DB@Alasdair这个答案是关于Postgresql@yossi我的意思是
描述“foo”@akaRem添加了表格创建和描述