question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Foreign key dropped on every sync and Unique Index created

See original GitHub issue

Issue 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

  1. Have a simple setup with two existing entities.
  2. Add a one-to-one relation between the entities
  3. Restart the script
  4. 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:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
danbrucecommented, Dec 18, 2019

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.

2reactions
jameshmreadcommented, Jan 22, 2021

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).

@Entity('StaffAvailabilities')
export class StaffAvailability {
  @PrimaryGeneratedColumn('uuid')
  id?: string;

  @ManyToOne(() => Shift, {
    nullable: false,
    primary: true,
    onDelete: 'NO ACTION',
  })
  shift?: Shift;

}
@Entity('Shifts')
export class Shift {
  @PrimaryGeneratedColumn('uuid')
  id?: string;

  @ManyToOne(
    () => Rota,
    r => r.shifts,
  )
  rota?: Rota;

}

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 the Shifts 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

60230: Synchronize model *always* want to alter foreign keys ...
So the Modeler thinks the foreignkey/index needs to be recreated because they're mismatched, but really they're just sorted differently. The way ...
Read more >
How to Index Foreign Key Columns in SQL Server
Retrieve all indexed foreign keys columns from all database tables by querying the sys.
Read more >
Cannot drop index because of foreign key constraint ...
Your index was created to optimize and enforce either a primary key or a unique key. Foreign keys from another tables that relate...
Read more >
Loose foreign keys - GitLab Docs
The tool ensures that all aspects of swapping a foreign key are covered. ... -c Re-creating current test database Dropped database 'gitlabhq_test_ee' ...
Read more >
Dependent foreign keys affected by Options - Redgate forums
SqlCompare's output script drops and recreates the primary key to rename it, but doesn't not correspondingly drop and rebuild the dependent ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found