Sqlite error: Foreign key Mismatch (fk incompatibils) when trying to make an insert

Asked

Viewed 720 times

0

I’m trying to create a Associative Entity (N:N) in Sqlite like this:

[ Pet --< Vacinapet >--- Vaccine ]

And, here is the code of my Associative Entity

CREATE TABLE  VACINAPET (
        vp_data TEXT NOT NULL,
        vp_is_aplicada INTEGER DEFAULT 0,
        fk_pet INTEGER,
        fk_vacina INTEGER,
        FOREIGN KEY (fk_pet) REFERENCES pet (pet_id),
        FOREIGN KEY (fk_vacina) REFERENCES vacina (vacina_id),
        PRIMARY KEY (fk_pet, fk_vacina) 
);

The code of my entity Pet:

CREATE TABLE PET (
        pet_id INTEGER PRIMARY KEY AUTOINCREMENT,
        pet_nome TEXT NOT NULL,
        pet_genero INTEGER DEFAULT 0,
        pet_foto_perfil TEXT,
        pet_peso INTEGER DEFAULT 0,
        pet_dt_nasc TEXT,
        fk_tpanimal INTEGER,
        fk_raca INTEGER,
        FOREIGN KEY (fk_tpanimal) REFERENCES TIPOANIMAL (tpanimal_id),
        FOREIGN KEY (fk_raca) REFERENCES RACA (raca_id)
);

And the code of my Vaccine entity:

CREATE TABLE VACINA (
        vac_id INTEGER PRIMARY KEY AUTOINCREMENT,
        vac_validade TEXT NOT NULL,
        fk_tipo INTEGER,
        FOREIGN KEY (fk_tipo) REFERENCES tipovacina (tpvac_id)
);

But I get that mistake here:

Foreign key Mismatch - "VACINAPET" referencing "vaccine": INSERT INTO VACINAPET (vp_data, vp_is_aplicada, fk_pet, fk_vacina) VALUES ('23/05/2018', 0, 1, 1);

When I try to use the following INSERT command:

INSERT INTO VACINAPET (vp_data, vp_is_applied, fk_pet, fk_vaccine) VALUES ('23/05/2018', 0, 1, 1);

What could be wrong?

NOTE: I have data in the tables Pet and Vaccine , they are not empty of data

  • Could add the structure of the other two tables!

  • Edited by @Nayronmorais

1 answer

1


The error is in the reference you are making in the association table (VACINAPET) of the VACCINE table, in the VACINAPET table you indicate as the primary key of the VACCINE table the field vacina_id, however, in the VACCINE table its primary key is named vac_id.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.