magento2: Can't create foreignKey with Declarative Schema on table not created through DS

Preconditions (*)

  1. Magento 2.3.0

Steps to reproduce (*)

  1. Create a module that uses Declarative Schema by creating a db_schema.xml
  2. Try to create a foreignKey with as referenceTable a table that isn’t created/controlled through Declarative Schema (in my case a 3rd party extension’s table)
  3. Run php bin/magento setup:upgrade

Expected result (*)

  1. Table is created with a foreign key to referenced table

Actual result (*)

  1. Error Notice: Undefined index: referenced_table_name_here in vendor/magento/framework/Setup/Declaration/Schema/Declaration/SchemaBuilder.php on line 352

When I edit vendor/magento/framework/Setup/Declaration/Schema/SchemaConfig.php:77 and add print_r(array_keys($data['table']));exit; there, I get a list of all tables created by extensions that use Declarative Schema. The referenced table isn’t one of them and thus setup:upgrade throws an error.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 14
  • Comments: 20 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@engcom-backlog-nazar If we read the documentation again and take what is explicitly written about this issue:

Declarative setup is based on database structure declarations, and is used in projects such as Doctrine. Schema files declare what the database structure should be, and Magento determines the differences between the current table structure and what it should be. These differences can be represented with atomic SQL operations.

Two things:

  • Magento determines the differences between the current table structure and what it should be. Sounds cristal clear to me.

  • and is used in projects such as Doctrine: Doctrine does that. It can create foreign keys with external tables not described in any of its description formats. By using the @JoinColumn annotation. Because Doctrine applies its migration scripts based on the real database structure. Not on its own representation of the database structure.

Closing this issue as there is no plans to implement external references for the db schema. The reference to the table added in a custom way should be added in a custom way as well.

This is not a feature request. This is implementing the backwards compatibility that was promised.

@engcom-backlog-nazar The sentence about FL_ALLOWED_SEVERITIES does not mention the fact that you cannot add a foreign key referencing a table not created through db_schema.xml… I read the sentence again and again… Nope.

Seems like an issue.

Like @peterjaap said, this is a serious impediment for the adoption of declarative schema.

@engcom-backlog-nazar but it’s not a feature? It’s a bug!

I meant adding it to the docs instead of that weird sentence about FL_ALLOWED_SEVERITIES.

Hi @peterjaap thank you for you report, It will be processed faster if you move this to https://github.com/magento/community-features

✅ Confirmed by @sdzhepa Thank you for verifying the issue. Based on the provided information internal tickets MC-18156 were created

Issue Available: @sdzhepa, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.