Skip to content

BUG: Migrations try to rename foreign key to a different table #3509

@ryuuji3

Description

@ryuuji3

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

A sample repository to follow.

Given the following entities Table1, Table2, and Table3 :

export class Table1 {
   @ManyToOne(type => Table2)
   table2Reference: Table2;
}

export class Table2 {
   @OneToMany(type => Table1, t1 => t1.table2Reference)
   table1References: Table1[];
}

export class Table3 {
   // ...
}

I generated a migration and ran it up and everything worked as expected. But then I needed to switch the reference in Table1 to Table3.

export class Table1 {
   @ManyToOne(type => Table3)
   table3Reference: Table3;
}

export class Table3 {
   @OneToMany(type => Table1, t1 => t1.table3Reference)
   table1References: Table1[];
}

The resulting migration opted to drop the foreign key constraint, RENAME table2Reference to table3Reference, and create new foreign key constraint that pointed to the right table.

The problem with that is that it breaks referential integrity. The foreign key points to an entirely different table. The old data should have been lost and it does not have anything to do with table3.

I apologize that this may not make sense so I'll prepare a repository this weekend.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions