[GH-ISSUE #41] Order of Create Tables is wrong when using foreign keys #26

Open
opened 2026-02-25 21:33:04 +03:00 by kerem · 4 comments
Owner

Originally created by @CodingPeak on GitHub (Mar 3, 2017).
Original GitHub issue: https://github.com/DBDiff/DBDiff/issues/41

Just a quick test resulted in 2 tables that should be created in a different order because the first references the second in a foreign key constraint.

CREATE TABLE permissions (
action_id int(11) NOT NULL,
user_type_id int(11) NOT NULL,
PRIMARY KEY (action_id,user_type_id),
KEY FK_permissions_user_types (user_type_id),
CONSTRAINT FK_permissions_actions FOREIGN KEY (action_id) REFERENCES sec_action (ActionId),
CONSTRAINT FK_permissions_user_types FOREIGN KEY (user_type_id) REFERENCES user_types (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE user_types (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Originally created by @CodingPeak on GitHub (Mar 3, 2017). Original GitHub issue: https://github.com/DBDiff/DBDiff/issues/41 Just a quick test resulted in 2 tables that should be created in a different order because the first references the second in a foreign key constraint. > CREATE TABLE `permissions` ( `action_id` int(11) NOT NULL, `user_type_id` int(11) NOT NULL, PRIMARY KEY (`action_id`,`user_type_id`), KEY `FK_permissions_user_types` (`user_type_id`), CONSTRAINT `FK_permissions_actions` FOREIGN KEY (`action_id`) REFERENCES `sec_action` (`ActionId`), CONSTRAINT `FK_permissions_user_types` FOREIGN KEY (`user_type_id`) REFERENCES `user_types` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; >CREATE TABLE `user_types` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Author
Owner

@ali4533 commented on GitHub (Mar 11, 2017):

+1

<!-- gh-comment-id:285875185 --> @ali4533 commented on GitHub (Mar 11, 2017): +1
Author
Owner

@geremora commented on GitHub (Nov 23, 2017):

We found the same situation. Generated script does not have dependency order.

<!-- gh-comment-id:346682722 --> @geremora commented on GitHub (Nov 23, 2017): We found the same situation. Generated script does not have dependency order.
Author
Owner

@llagerlof commented on GitHub (Nov 24, 2017):

Same issue here.

<!-- gh-comment-id:346816818 --> @llagerlof commented on GitHub (Nov 24, 2017): Same issue here.
Author
Owner

@jasdeepkhalsa commented on GitHub (Nov 24, 2017):

Thanks for pointing this out with an example! I can see why this could be problematic - it's also a complex problem to solve.

Essentially DBDiff needs to know exactly which tables have foreign key constraints in another table and use that information to effectively sort the order of the output SQL migration statements

<!-- gh-comment-id:346848149 --> @jasdeepkhalsa commented on GitHub (Nov 24, 2017): Thanks for pointing this out with an example! I can see why this could be problematic - it's also a complex problem to solve. Essentially DBDiff needs to know exactly which tables have foreign key constraints in another table and use that information to effectively sort the order of the output SQL migration statements
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/DBDiff#26
No description provided.