magento2: Can't create foreignKey with Declarative Schema on table not created through DS
Preconditions (*)
- Magento 2.3.0
Steps to reproduce (*)
- Create a module that uses Declarative Schema by creating a
db_schema.xml - Try to create a
foreignKeywith asreferenceTablea table that isn’t created/controlled through Declarative Schema (in my case a 3rd party extension’s table) - Run
php bin/magento setup:upgrade
Expected result (*)
- Table is created with a foreign key to referenced table
Actual result (*)
- 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
- Replacing declarative schema by setup Because of https://github.com/magento/magento2/issues/21770 — committed to botisSmile/elasticsuite by rbayet 5 years ago
@engcom-backlog-nazar If we read the documentation again and take what is explicitly written about this issue:
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-18156were createdIssue 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.