Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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/22.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=真,不工作_Python_Django - Fatal编程技术网

Python Django unique=真,不工作

Python Django unique=真,不工作,python,django,Python,Django,这来自django的文档: Field.unique 如果为True,则此字段在整个表中必须是唯一的 这是在数据库级别和通过模型验证实施的。 如果试图在唯一字段中保存具有重复值的模型,则会出现django .db.IntegrityError将由模型的save()方法引发 这是我的模特 class MyModel(models.Model): # my pk is an auto-incrementing field url = models.URLField("URL

这来自django的文档:

Field.unique

如果为True,则此字段在整个表中必须是唯一的

这是在数据库级别和通过模型验证实施的。 如果试图在唯一字段中保存具有重复值的模型,则会出现django .db.IntegrityError将由模型的save()方法引发

这是我的模特

class MyModel(models.Model):
    # my pk is an auto-incrementing field
    url = models.URLField("URL", unique=True)
    text = models.TextField(max_length=1000)
    # my model is just two fields, one pk (unique), and another unique field, 
    #, the url
这里是manage.py sqlall(我运行了syncdb)

但是,在manage.py shell中,我可以自由地执行以下操作:

>>> from MyModel.models import MyModel
>>> MyModel().save() # it works fine!? Not even the text was checked for!
>>> MyModel(url="blah").save() 
>>> MyModel(url="blah").save() # it still works!

# I checked the mysql database afterwards, the models were saved just fine, they
# however did have different PK's (auto incrementing fields).
我正在使用mysql,django 1.5。有没有人知道是什么原因造成的

我使用的是定制管理器,但我怀疑这是问题所在


谢谢。

感谢django 1.9+
运行
makemigrations
然后
migrate
将唯一约束应用于sqlite3

对于django<1.9
由于您使用的是django 1.5,因此将应用此解决方案

如果在创建表之后添加了
unique=True
,则即使稍后执行
syncdb
,也不会将唯一条件添加到表中

我可以用
sqlite3
确认Django 1.5很高兴地用
MyModel(url=“blah”).save()保存重复的对象,如果数据库中不存在唯一约束,这似乎与文档相矛盾

最好的解决方案是使用此命令在数据库中手动创建约束

ALTER TABLE MyModel_mymodel ADD UNIQUE (url);

或者如果你不介意,你可以重新创建你的桌子。(删除该表,然后运行
syncdb

可以避免直接在数据库上运行sql脚本。而是在迁移中添加sql执行:

from __future__ import unicode_literals
from django.db import migrations, connection


def alter_table(apps, schema_editor):
    query ="ALTER TABLE <your table> ADD UNIQUE (unique_col);"
    cursor = connection.cursor()
    cursor.execute(query)
    cursor.close()

class Migration(migrations.Migration):

    dependencies = [
        ('app', 'yourlastmigration'),
    ]

    operations = [        
        migrations.RunPython(alter_table),
    ]
从未来导入unicode文本
从django.db导入迁移,连接
def alter_表(应用程序、架构编辑器):
query=“ALTER TABLE ADD UNIQUE(UNIQUE_col);”
cursor=connection.cursor()
cursor.execute(查询)
cursor.close()
类迁移(migrations.Migration):
依赖项=[
('app','yourlastmigation'),
]
操作=[
RunPython(alter_表),
]

解决方案非常简单,只需按照他们的步骤操作即可

1 - Dell all the files in the migration folder
2 - Then run the command "python manage.py makemigrations"
3 - Then run the command "python manage.py migrate"

通过一个简单的SQLLite查询添加索引示例来实现

alter table test添加索引index_name(col1(255),col2(255))

添加唯一索引示例


altertable测试添加唯一索引_名称(col1(255),col2(255))

我得到了
IntegrityError:第二个
MyModel(url=“blah”).save()的列url不是唯一的
异常。是否按原样粘贴代码?
文本
字段定义有输入错误<代码>模式
。我怀疑你没有按原样复制和粘贴你的代码。不,我没有,不要认为代码需要按原样发布,只有两个字段是值得注意的。谢谢,它可以工作。我希望django在很多db方面更容易,比如更改和删除FK等。在django 1.9.4中,惟一条件被添加到表中(使用sqlite3)-即使已经创建了表。
Django 1.8
mysql
如果在创建表后向模型字段添加
unique=True
,也不会创建唯一的条目。对Django版本的概述以及关于unique=working的数据库将很有帮助?!我也很困惑。需要注意的是,在admin界面中进行的模型验证在save()期间不会发生。相反,保存期间的唯一性仅在数据库级别强制执行。如果我删除了所有字段,那么如何获取我以前的数据?@Shamim您的数据位于_sql数据库或其他不在迁移文件夹中的数据库中,因此您的数据是安全的。
1 - Dell all the files in the migration folder
2 - Then run the command "python manage.py makemigrations"
3 - Then run the command "python manage.py migrate"