I am trying to add a new foriegn key in existing table using db_schema.xml but it is not working at all I tried to delete the table and delete module from setup_module table as well but still nothing working.

How I can resolve this issue? Thanks!!

1 Answer 1


Below is an example of using the foreign key as product id with a custom table.

<table name="custom_tab" resource="default" engine="innodb">
  <column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true"/>
  <column name="product_id" nullable="true" xsi:type="int" padding="10" identity="false" unsigned="true"/>
  <constraint xsi:type="primary" referenceId="PRIMARY">
    <column name="entity_id"/>
  <constraint xsi:type="foreign" referenceId="CUSTOM_TABLE_PRODUCT_ID_CATALOG_PRODUCT_ENTITY_ENTITY_ID"
    table="custom_tab" column="product_id"
   referenceTable="catalog_product_entity" referenceColumn="entity_id" onDelete="CASCADE"/>

Please try like this and inform it is working for you or not ?

  • It is still not working actually it works on loaclhost but giving error on live server of di:compile , Commented Feb 23, 2023 at 13:20
  • can you please send me the error ? Commented Feb 27, 2023 at 4:54
  • It's done I truncated all the tables and then run upgrade commands !! Commented Feb 27, 2023 at 5:53
  • 1
    I think old key of your row is not consider, so after delete table it is working. Commented Feb 27, 2023 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.