Foreign key dropped on every sync and Unique Index created
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql
/ mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem: When adding a one-to-one relation between two entities, a foreign key will be recreated on every startup after the one-to-one relation is created. Also a unique index is recreated on every startup after being created with the relation.
When the entities with relation are created in an empty database, then to foreign key will not be recreated on startup and the unique index won’t be created at all.
Steps to reproduce
- Have a simple setup with two existing entities.
- Add a one-to-one relation between the entities
- Restart the script
- The same SQL with ALTER queries will run on startup each time.
Reproduction Gist: https://gist.github.com/Ionaru/9d01b6e5538e577bcea5e694728eee78
START TRANSACTION
SELECT DATABASE() AS `db_name`
SELECT * FROM `INFORMATION_SCHEMA`.`TABLES` WHERE (`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `TABLE_NAME` = 'foo') OR (`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `TABLE_NAME` = 'bar')
SELECT * FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `TABLE_NAME` = 'foo') OR (`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `TABLE_NAME` = 'bar')
SELECT * FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` WHERE `CONSTRAINT_NAME` = 'PRIMARY' AND ((`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `TABLE_NAME` = 'foo') OR (`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `TABLE_NAME` = 'bar'))
SELECT `SCHEMA_NAME`, `DEFAULT_CHARACTER_SET_NAME` as `CHARSET`, `DEFAULT_COLLATION_NAME` AS `COLLATION` FROM `INFORMATION_SCHEMA`.`SCHEMATA`
SELECT `s`.* FROM `INFORMATION_SCHEMA`.`STATISTICS` `s` LEFT JOIN `INFORMATION_SCHEMA`.`REFERENTIAL_CONSTRAINTS` `rc` ON `s`.`INDEX_NAME` = `rc`.`CONSTRAINT_NAME` WHERE ((`s`.`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `s`.`TABLE_NAME` = 'foo') OR (`s`.`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `s`.`TABLE_NAME` = 'bar')) AND `s`.`INDEX_NAME` != 'PRIMARY' AND `rc`.`CONSTRAINT_NAME` IS NULL
SELECT `kcu`.`TABLE_SCHEMA`, `kcu`.`TABLE_NAME`, `kcu`.`CONSTRAINT_NAME`, `kcu`.`COLUMN_NAME`, `kcu`.`REFERENCED_TABLE_SCHEMA`, `kcu`.`REFERENCED_TABLE_NAME`, `kcu`.`REFERENCED_COLUMN_NAME`, `rc`.`DELETE_RULE` `ON_DELETE`, `rc`.`UPDATE_RULE` `ON_UPDATE` FROM `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` `kcu` INNER JOIN `INFORMATION_SCHEMA`.`REFERENTIAL_CONSTRAINTS` `rc` ON `rc`.`constraint_name` = `kcu`.`constraint_name` WHERE (`kcu`.`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `kcu`.`TABLE_NAME` = 'foo') OR (`kcu`.`TABLE_SCHEMA` = 'web_evetrack_test_2' AND `kcu`.`TABLE_NAME` = 'bar')
DROP INDEX `IDX_87b3a1a751e750a943188661ec` ON `foo`
ALTER TABLE `foo` DROP FOREIGN KEY `FK_87b3a1a751e750a943188661ec0`
ALTER TABLE `foo` ADD UNIQUE INDEX `IDX_87b3a1a751e750a943188661ec` (`barId`)
ALTER TABLE `foo` ADD CONSTRAINT `FK_87b3a1a751e750a943188661ec0` FOREIGN KEY (`barId`) REFERENCES `bar`(`id`)
COMMIT
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:13 (2 by maintainers)
Top GitHub Comments
This is still happening for me on
0.2.20
with MySQL. Every time I synchronize, my unique index that includes a foreign key tries to get dropped and recreated. Since it’s the only index on that foreign key, MySQL complains about dropping a required index for a foreign key.Seeing this issue in 0.2.30 as well. I was struggling with this for hours. It appears to be something to do with the @PrimaryGeneratedColumn(‘uuid’) decorator. I’m not exactly sure what is going on but hope this can point maintainers in the right direction. My entities (other columns removed for brevity).
Running on MySQL: 8.0.18-google (hosted on GCloud MySQL) The ORM would create the tables just fine and would rebuild and re-sync fine. Until I inserted items into the
StaffAvailabilities
table. On the Next restart with no code changes. It would drop the Foreign key referencing theShifts
table and then complain that the reference didn’t exist.The only thing that worked was to change the
@PrimaryGeneratedColumn('uuid')
in both files. To this@PrimaryColumn({ generated: 'uuid' })
drop both tables and then recompile and re-sync. Then the issue is fixed.