Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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-Postgress-仅当不';不存在_Python_Django_Postgresql_Unique Constraint - Fatal编程技术网

Python Django-Postgress-仅当不';不存在

Python Django-Postgress-仅当不';不存在,python,django,postgresql,unique-constraint,Python,Django,Postgresql,Unique Constraint,我想创建一个元素,但前提是“相似”对象不存在。例如: name_to_be_created = "John" age_to_be_created = 15 if not Person.objects.filter(name=name_to_be_created, age__gte=age_to_be_created).exists(): Person.objects.create(name=name_to_be_created, age=age_to_be_created) 请注意,条

我想创建一个元素,但前提是“相似”对象不存在。例如:

name_to_be_created = "John"
age_to_be_created = 15
if not Person.objects.filter(name=name_to_be_created, age__gte=age_to_be_created).exists():
    Person.objects.create(name=name_to_be_created, age=age_to_be_created)
请注意,条件故意不够简单,不允许使用
get\u或\u create

据我所知,在使用
select
insert
时使用事务并不能保证这种一致性


我正在使用Postgres

嗨,问题是什么?唯一的条件是
要创建的年龄必须大于该
人的最新退出行
?如果您担心多个应用程序的竞争条件,请使用select\u for\u update在DB中使用锁定