简体   繁体   English

Postgresql错误:没有唯一约束匹配给定键的引用表

[英]Postgresql ERROR: there is no unique constraint matching given keys for referenced table

I have two tables "Persona" (Person) and "Dona" (Female) and I want to alter a third one named "Baptisme" (Baptism), but I'm having some problems. 我有两个表“ Persona”(人)和“ Dona”(女),我想更改第三个表,名称为“ Baptisme”(洗礼),但是我遇到了一些问题。

These are my tables: 这些是我的表:

CREATE TABLE baptismes.Persona (
    id SERIAL PRIMARY KEY,
    nom VARCHAR(255),
    nom_complementari1 VARCHAR(255),
    nom_complementari2 VARCHAR(255),
    nom_complementari3 VARCHAR(255),
    nom_complementari4 VARCHAR(255),
    cognom1 VARCHAR(255),
    cognom2 VARCHAR(255),
    lloc_naixement VARCHAR(255) REFERENCES baptismes.Poblacio(nom),
    data_naixement baptismes.Data,
    alies VARCHAR(255),
    sexe CHAR(1),
    difunt BOOLEAN
);

CREATE TABLE baptismes.Dona (
    cognom_marit_actual VARCHAR(255),
    cognom_marit_anterior VARCHAR(255)
) INHERITS (baptismes.Persona);

CREATE TABLE baptismes.Baptisme (
    id SERIAL PRIMARY KEY,
    ...
);

Baptisme has more attributes but they doesn't matter here. 洗礼具有更多的属性,但在这里并不重要。

What I'm trying to do is: 我想做的是:

ALTER TABLE baptismes.baptisme
ADD COLUMN mare INTEGER REFERENCES baptismes.Dona(id);

but this gives me the following error: 但这给了我以下错误:

ERROR:  no hay restricción unique que coincida con las columnas dadas en la tabla referida «dona»
********** Error **********

ERROR: no hay restricción unique que coincida con las columnas dadas en la tabla referida «dona»
SQL state: 42830

In english: "there's no unique constraint matching given keys for referenced table <>". 用英语来说:“没有唯一的约束匹配给定表<>的给定键。”

I can't understand because Dona gets PK from Persona (id), so it should be UNIQUE . 我不明白,因为Dona从Persona(id)获得了PK,所以应该是UNIQUE

Can somebody help me, please? 有人可以帮我吗? Thanks! 谢谢!

Alas, this is a documented deficiency: las,这是有据可查的缺陷:

5.8.1. 5.8.1。 Caveats 注意事项

A serious limitation of the inheritance feature is that indexes (including unique constraints) and foreign key constraints only apply to single tables, not to their inheritance children. 继承功能的一个严重限制是索引(包括唯一约束)和外键约束仅适用于单个表,不适用于它们的继承子级。 This is true on both the referencing and referenced sides of a foreign key constraint. 在外键约束的引用端和被引用端均是如此。

  • If we declared cities.name to be UNIQUE or a PRIMARY KEY , this would not stop the capitals table from having rows with names duplicating rows in cities. 如果我们将cities.name声明为UNIQUEPRIMARY KEY ,则这不会阻止大写cities.name行名称与城市中的行重复。 And those duplicate rows would by default show up in queries from cities. 默认情况下,这些重复的行将显示在城市查询中。 In fact, by default capitals would have no unique constraint at all, and so could contain multiple rows with the same name. 实际上,默认情况下,大写字母根本没有唯一的约束,因此可以包含多个具有相同名称的行。 You could add a unique constraint to capitals, but this would not prevent duplication compared to cities. 您可以向首都添加唯一的约束,但是与城市相比,这不能防止重复。

So, your problem is that dona.id doesn't have a unique constraint. 因此,您的问题是dona.id没有唯一约束。 I think you can declare one in the create table statement -- or by altering the table afterwards. 我认为您可以在create table语句中声明一个-或在以后更改表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PostgreSQL错误:没有唯一约束匹配给定键的引用表 - PostgreSQL Error: there is not unique constraint matching given keys for referenced table 没有与引用表“用户”的给定键匹配的唯一约束(PostgreSQL) - No unique constraint matching given keys for referenced table “users” (PostgreSQL) 数据库原型 Postgresql:错误:没有唯一约束匹配给定键的引用表“消息” - Database prototype Postgresql: ERROR: there is no unique constraint matching given keys for referenced table “messages” 没有唯一约束匹配给定引用表的键 - No unique constraint matching given keys for referenced table PSQL错误,没有唯一约束匹配给定表引用的键 - PSQL Error there is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定键的引用表 - ERROR: there is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定键的引用表“bar” - ERROR: there is no unique constraint matching given keys for referenced table "bar" 远离错误:没有唯一约束匹配引用表的给定键 - Away around Error: There is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定表的键 - ERROR: No unique constraint matching given keys for referenced table 如何修复错误“没有唯一约束匹配引用表的给定键” - How to fix error "there is no unique constraint matching given keys for referenced table"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM