Question

I try to add 2 foreign key to my table using db_schema.xml

<table name="test_table" engine="innodb" resource="default" >
    <column xsi:type="smallint" name="entity_id" identity="true" nullable="false" unsigned="false"/>
    <column xsi:type="smallint" name="id_test" unsigned="false" nullable="false" identity="false"/>
    <column xsi:type="smallint" name="id_user" unsigned="false" nullable="false" identity="false"/>
    <column xsi:type="varchar" name="title" length="255" unsigned="false" nullable="false" identity="false"/>
    <constraint xsi:type="primary" referenceId="PRIMARY">
        <column name="entity_id" />
    </constraint>

    <constraint xsi:type="foreign" referenceId="TEST_TEST_ID_" table="test_table" column="id_test" referenceTable="second_test_table" referenceColumn="entity_id" onDelete="CASCADE"/>
    <constraint xsi:type="foreign" referenceId="TEST_USER" table="test_table" column="id_user" referenceTable="admin_user" referenceColumn="user_id" onDelete="CASCADE"/>
</table>

but always I get this error: " (errno: 150 "Foreign key constraint is incorrectly formed" "

After a lot of try I found that the error is the user_id foreign key, and the error message is :

Column definition "id_user" and reference column definition "user_id" are different in tables "test_table" and "admin_user"

Was it helpful?

Solution

Change by following code:

<column xsi:type="int" name="id_user" padding="10" unsigned="true" nullable="true" identity="false"
            comment="User ID"/>

Column definition should be same if you use as a foreign key.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top