Answer the question
In order to leave comments, you need to log in
How to fix InvalidForeignKey error?
Explain how to fix this error with FK.
The code:
cur.execute('''CREATE TABLE Vm
(id_vm INT PRIMARY KEY NOT NULL,
uuid_vm CHAR(100) NOT NULL,
name_vm CHAR(100) NOT NULL,
bg_date DATE NOT NULL,
end_date DATE DEFAULT('2999-05-12'))
''')
cur.execute('''CREATE TABLE Vm_parm
(id_parm INT PRIMARY KEY NOT NULL,
title CHAR(30) NOT NULL,
type CHAR(10) NOT NULL)
''')
cur.execute('''CREATE TABLE Vm_stat
(id_stat INT PRIMARY KEY NOT NULL,
uuid_vm CHAR(100) NOT NULL,
name_vm CHAR(100) NOT NULL,
bg_date DATE NOT NULL,
end_date DATE NOT NULL,
vm_parm CHAR(30) NOT NULL,
value_parm INT NOT NULL)
''')
cur.execute("ALTER TABLE Vm_stat ADD CONSTRAINT fk_Vm_stat_uuid_vm_name_vm_bg_date_end_date FOREIGN KEY(uuid_vm, name_vm, bg_date, end_date)"
"REFERENCES Vm (uuid_vm, name_vm, bg_date, end_date)")
cur.execute("ALTER TABLE Vm_stat ADD CONSTRAINT fk_Vm_stat_vm_parm FOREIGN KEY(title)"
"REFERENCES Vm_parm(title)")
line 34, in <module>
cur.execute("ALTER TABLE Vm_stat ADD CONSTRAINT fk_Vm_stat_uuid_vm_name_vm_bg_date_end_date FOREIGN KEY (uuid_vm, name_vm, bg_date, end_date)"
psycopg2.errors.InvalidForeignKey: there is no unique constraint matching given keys for referenced table "vm"
Answer the question
In order to leave comments, you need to log in
Well, he writes in English - there is no unique constraint on the group of fields that you refer to (uuid_vm, name_vm, bg_date, end_date).
In general, they piled up a strange scheme: if there is a surrogate key id_vm, why keep a bunch of fields in both tables, and even make a foreign key on them?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question