Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreign key constraint incorrectly formed but for the same type (Varchar(64))

I’ve created 2 tables for my mariadb (item_general_info and recieving_item) with the 2nd table has a foreign key constraint, but the foreign key contraint cause an error (errno: 150 "Foreign key constraint is incorrecly formed")

here’s the query for the item_general_info:

CREATE TABLE Item_General_Info (UID VARCHAR(6) PRIMARY KEY,
                                name VARCHAR(64) NOT NULL,
                                Category VARCHAR(64) NOT NULL,
                                does_expire INT(1) NOT NULL) COLLATE LATIN1_GENERAL_CI;

and here’s the recieving_item:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CREATE TABLE recieving_item(NAME VARCHAR(64) NOT NULL REFERENCES item_general_info.name,
                            stock INT(11) NOT NULL,
                            supp_name VARCHAR(64) NOT NULL,
                            recieving_data DATE NOT NULL,
                            reciever VARCHAR(128) NOT NULL COLLATE LATIN1_GENERAL_CS,
                            state INT (1) NOT NULL)COLLATE = latin1_general_ci

they’re both varchar(64) and even had the same collation and both are can’t be null, but there’s still an error, any reasons why?

make a successful foreign key constraint from the 2nd table

>Solution :

The problem with your foreign key restriction is that you are attempting to use a certain column of the item_general_info table as a key even though it is not declared as such. A unique column or a primary key column of the parent table should be the target of the foreign key. Since the name column in item_general_info is not unique nor the table’s primary key, you are attempting to refer to it in this instance.

In the item_general_info table, a new primary key column has to be defined in order to address issue. The table’s PRIMARY KEY may be changed to a new column named id. Below is how you can modify the item_general_info table:

CREATE TABLE Item_General_Info (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    UID VARCHAR(6) UNIQUE,
    name VARCHAR(64) NOT NULL,
    Category VARCHAR(64) NOT NULL,
    does_expire INT(1) NOT NULL
) COLLATE LATIN1_GENERAL_CI;

recieving_item table:

CREATE TABLE recieving_item(
    item_id INT(11) NOT NULL,
    stock INT(11) NOT NULL,
    supp_name VARCHAR(64) NOT NULL,
    recieving_data DATE NOT NULL,
    reciever VARCHAR(128) NOT NULL COLLATE LATIN1_GENERAL_CS,
    state INT (1) NOT NULL,
    FOREIGN KEY (item_id) REFERENCES item_general_info(id)
) COLLATE = latin1_general_ci;

The foreign key for the receiving_item table is the new id column of the item_general_info table. The foreign key constraint problem should be resolved as a result.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading