vapor / fluent-mysql-driver

🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
MIT License
78 stars 52 forks source link

References and Foreign Key Constraints not being applied #170

Closed 0xTim closed 4 years ago

0xTim commented 4 years ago

I'm setting up a reference in Fluent between two tables here. In Postgres, this sets up a FKC so that I can't create a post if the user doesn't exist, as defined in this test. However, on MySQL this isn't setting up any FKC and the test fails.

This is the SQL the migration produces:

2020-04-29T15:23:19+0100 debug: database-id=mysql CREATE TABLE `BlogPost`(`postID` BIGINT PRIMARY KEY AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `contents` VARCHAR(255) NOT NULL, `author` BIGINT NOT NULL REFERENCES `BlogUser` (`userID`) ON DELETE NO ACTION ON UPDATE NO ACTION, `created` DATETIME(6) NOT NULL, `lastEdited` DATETIME(6), `slugUrl` VARCHAR(255) NOT NULL, `published` BOOL NOT NULL, CONSTRAINT `d9fbf3c95bb905bc68d282cb6b0ac0415401fdc4` UNIQUE (`slugUrl`)) []
tanner0101 commented 4 years ago

Hmm looks like the REFERENCES statement is getting serialized correctly

`author` BIGINT NOT NULL REFERENCES `BlogUser` (`userID`)

MySQL must need some special formatting there.

0xTim commented 4 years ago

Yeah, I'm seeing that as well, but when looking at the database keys, it's not setting up any foreign keys when it should for MySQL at the same time - at least according to this post