[PR #440] [CLOSED] The schema tool now doesn't add a foreign constraint when subclassess of... #8198

Closed
opened 2026-01-22 15:58:51 +01:00 by admin · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/doctrine/orm/pull/440
Author: @sroddy
Created: 9/8/2012
Status: Closed

Base: masterHead: fix_STI_schema_tool


📝 Commits (2)

  • 0fa3bff adedd failing test for PR #440
  • daaa87b The schema tool now doesn't add a foreign constraint when subclassess of a STI use the same field to map relations with entities of different classes

📊 Changes

7 files changed (+357 additions, -12 deletions)

View changed files

📝 lib/Doctrine/ORM/Tools/SchemaTool.php (+38 -12)
tests/Doctrine/Tests/Models/SingleTableInheritanceType/Structure.php (+22 -0)
tests/Doctrine/Tests/Models/SingleTableInheritanceType/User.php (+124 -0)
tests/Doctrine/Tests/Models/SingleTableInheritanceType/UserFollowedObject.php (+32 -0)
tests/Doctrine/Tests/Models/SingleTableInheritanceType/UserFollowedStructure.php (+54 -0)
tests/Doctrine/Tests/Models/SingleTableInheritanceType/UserFollowedUser.php (+55 -0)
📝 tests/Doctrine/Tests/ORM/Tools/SchemaToolTest.php (+32 -0)

📄 Description

... a STI use the same field to map relations with entities of different classes

It seems that there are no particular side-effects in mapping different relationship using the same table column name in different subclasses of a STI hierarchy.
Eg. Tag superclass of BookTag (id, object_id, comment) and MovieTag (id, object_id, comment), so having two attributes $book and $movie mapped to the same object_id field with @JoinColumn annotation

I wasn't sure about this so I tested to see if there were errors. And I was surprised that everything runs without any particular problem.
Moreover this is a really interesting (wanted? unwanted?) feature, very useful if you have huge tables that need to be joined with other table using the discriminator and object_id columns, or if you have some kind of logging class and you need to keep the table efficient and small. The only problem is that the schema generator always inserts a foreign column constraint (probably the last specified) when generating these kind of Entities.

This PR ensures that the foreign key is not added if there are at least two different classes mapped on the same field of a db table.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/doctrine/orm/pull/440 **Author:** [@sroddy](https://github.com/sroddy) **Created:** 9/8/2012 **Status:** ❌ Closed **Base:** `master` ← **Head:** `fix_STI_schema_tool` --- ### 📝 Commits (2) - [`0fa3bff`](https://github.com/doctrine/orm/commit/0fa3bff343ed52540983b599501d3602bf458787) adedd failing test for PR #440 - [`daaa87b`](https://github.com/doctrine/orm/commit/daaa87bc5d52d50ef336bd72e78cae81b10a0aa7) The schema tool now doesn't add a foreign constraint when subclassess of a STI use the same field to map relations with entities of different classes ### 📊 Changes **7 files changed** (+357 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `lib/Doctrine/ORM/Tools/SchemaTool.php` (+38 -12) ➕ `tests/Doctrine/Tests/Models/SingleTableInheritanceType/Structure.php` (+22 -0) ➕ `tests/Doctrine/Tests/Models/SingleTableInheritanceType/User.php` (+124 -0) ➕ `tests/Doctrine/Tests/Models/SingleTableInheritanceType/UserFollowedObject.php` (+32 -0) ➕ `tests/Doctrine/Tests/Models/SingleTableInheritanceType/UserFollowedStructure.php` (+54 -0) ➕ `tests/Doctrine/Tests/Models/SingleTableInheritanceType/UserFollowedUser.php` (+55 -0) 📝 `tests/Doctrine/Tests/ORM/Tools/SchemaToolTest.php` (+32 -0) </details> ### 📄 Description ... a STI use the same field to map relations with entities of different classes It seems that there are no particular side-effects in mapping different relationship using the same table column name in different subclasses of a STI hierarchy. Eg. Tag superclass of BookTag (id, object_id, comment) and MovieTag (id, object_id, comment), so having two attributes $book and $movie mapped to the same object_id field with @JoinColumn annotation I wasn't sure about this so I tested to see if there were errors. And I was surprised that everything runs without any particular problem. Moreover this is a really interesting (wanted? unwanted?) feature, very useful if you have huge tables that need to be joined with other table using the discriminator and object_id columns, or if you have some kind of logging class and you need to keep the table efficient and small. The only problem is that the schema generator always inserts a foreign column constraint (probably the last specified) when generating these kind of Entities. This PR ensures that the foreign key is not added if there are at least two different classes mapped on the same field of a db table. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
admin added the pull-request label 2026-01-22 15:58:51 +01:00
admin closed this issue 2026-01-22 15:58:51 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#8198