Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Two hook_schema() implementations define foreign key constraints referring to tables which are defined later in the same hook_schema() implementation. This makes using foreign key constraints difficult. The attached patch re-arranges the order that the tables are defined in inside the two hook_schema() implementations. This fixes and the problem and should have no side-effects.
Comment | File | Size | Author |
---|---|---|---|
#2 | core_reorder_schema.patch | 11.38 KB | Liam Morland |
#1 | core_reorder_schema.patch | 11.33 KB | Liam Morland |
core_reorder_schema.patch | 11.33 KB | Liam Morland | |
Comments
Comment #1
Liam MorlandReroll.
Comment #2
Liam MorlandReroll.
Comment #3
ParisLiakos CreditAttribution: ParisLiakos commentedpretty straightforward, just rearranging code
Comment #4
webchickConfirmed this is just moving some code around.
Committed and pushed to 8.x. Thanks!
Comment #5
webchickOops. :)
Comment #6
sun1) There are no references in functional code between schema definitions. The order of definition does not matter at all.
2) If any "order" can and should be applied, then that order is alphabetical. Which apparently was the case for most hook_schema() definitions already. This change broke that.
3) This change could have used some more reviews before commit. Especially since it requires a couple of major patches in the queue to be rerolled.
Comment #7
Liam MorlandThe order does matter if foreign key constraints are being created. Foreign key constraints can only refer to tables that already exist. The reorder in this patch ensured that tables only refer to other tables that already exist.
This patch helps with #911352: Document that foreign keys may not be used by all drivers.
Comment #8
webchickRight, and even if it didn't help with that patch (which I'm still not totally convinced we should do in core), it does help with the understanding of the database schema for a first-time reader. Alphabetical is a totally arbitrary way to outline schema definitions; "natural order" (where you learn what a vocabulary is *before* you see references to it in other tables) makes a lot more sense.
If you have major patches that are close to RTBC which you don't want to be broken by cleanup patches, we have a process for that; tag them "Avoid commit conflicts." there are no patches that touch taxonomy.install in the list.
Comment #9
Liam Morland@webchick: That is a good way of explaining it. Creating tables before referring to them makes sense for people as well as computers.