我在postgres USER表上应用了unique constraint,而在email上应用了unique constraint,我现在面临的问题是,约束似乎注册了我插入(或尝试插入)的每个值,而不管具有该值的记录是否存在。
表格:
| 身份证|用户|
| - ------|- ------|
| 1个|www.example.commail@gmail.com|
| 第二章|www.example.commail2@gmail.com|
如果我插入www.example.com,请删除该值,然后尝试再次插入mail3@gmail.com,它将显示:mail3@gmail.com, delete the value and try to insert mail3@gmail.com again it says:
sqlalchemy.exc.IntegrityError:(psycopg2.errors.UniqueViolation)重复的键值违反了唯一约束"email"
我的怀疑是:唯一约束保证值总是较新的,或者在列中只有该值的一个记录?
文件上说是第二个,但经验表明是第一个
更多详情:
| Column | Type | Nullable |
|------------------|-----------------------------|----------|
| id | integer | not null |
| email | character varying(100) | |
| password | character varying(100) | |
| name | character varying(1000) | |
| lastname | character varying(1000) | |
| dni | character varying(20) | |
| cellphone | character varying(20) | |
| accepted_terms | boolean | |
| investor_test | boolean | |
| validated_email | boolean | |
| validated_cel | boolean | |
| last_login_at | timestamp without time zone | |
| current_login_at | timestamp without time zone | |
| last_login_ip | character varying(100) | |
| current_login_ip | character varying(100) | |
| login_count | integer | |
| active | boolean | |
| fs_uniquifier | character varying(255) | not null |
| confirmed_at | timestamp without time zone | |
Indexes:
"bondusers_pkey" PRIMARY KEY, btree (id)
"bondusers_email_key" UNIQUE CONSTRAINT, btree (email)
"bondusers_fs_uniquifier_key" UNIQUE CONSTRAINT, btree (fs_uniquifier)
插入声明:
INSERT INTO bondusers (email, password, name, lastname, dni, cellphone, accepted_terms, investor_test, validated_email, validated_cel, last_login_at, current_login_at, last_login_ip, current_login_ip, login_count, active, fs_uniquifier, confirmed_at) VALUES ('mail3@gmail.com', '$pbkdf2-sha256$29000$XyvlfI8x5vwfYwyhtBYi5A$Hhfrzvqs94MjTCmDOVmmnbUyf7ho4kLEY8UYUCdHPgM', 'mail', 'mail3', '123123123', '1139199196', false, false, false, false, NULL, NULL, NULL, NULL, NULL, true, '1c4e60b34a5641f4b560f8fd1d45872c', NULL);
ERROR: duplicate key value violates unique constraint "bondusers_fs_uniquifier_key"
DETAIL: Key (fs_uniquifier)=(1c4e60b34a5641f4b560f8fd1d45872c) already exists.
但当:
select * from bondusers where fs_uniquifier = '1c4e60b34a5641f4b560f8fd1d45872c';
结果为0行
1条答案
按热度按时间2guxujil1#
我假设如果您直接在Postgres命令行中运行INSERT、DELETE、INSERT,它可以正常工作吗?
我注意到你的错误引用了SQLAlchemy(
sqlalchemy.exc.IntegrityError
),所以我认为可能是因为这个原因,而不是PostgreSQL。在事务中,SQLAlchemy的工作单元模式可以出于性能原因重新排序SQL语句。我能找到的唯一参考文献是https://github.com/sqlalchemy/sqlalchemy/issues/5735#issuecomment-735939061:
如果在目标表之间没有相关性循环,则刷新如下进行:
因此,如果您在单个事务中有以下内容:
当你提交它的时候,它可能会被重新排序为:
我在Java/hib中对这个问题有更多的经验。SQLAlchemy文档确实声称它的工作单元模式是 “模仿Fowler的“Unit of Work”模式以及Hibernate,Java的主要对象关系Map器。" 所以可能在这里也相关