Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The names of foreign keys created by queryRunner.createForeignKey and schema:sync are different with SQLite #9176

Closed
fossamagna opened this issue Jul 4, 2022 · 0 comments · Fixed by #9185

Comments

@fossamagna
Copy link
Contributor

Issue Description

In typeorm@0.3.7, there will be a difference in the name of the foreign key created by queryRunner.createForeignKey and typeorm schema:sync.

Expected Behavior

In typeorm@0.3.7, the name of the foreign key created by queryRunner.createForeignKey and schema:sync is the same.

Actual Behavior

Outputs when run npm migration:check:

Unexpected changes in database schema were found in check mode:

import { MigrationInterface, QueryRunner } from "typeorm";

export class dummy1656941197920 implements MigrationInterface {
    name = 'dummy1656941197920'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE "temporary_post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" varchar NOT NULL, "authorId" integer NOT NULL)`);
        await queryRunner.query(`INSERT INTO "temporary_post"("id", "title", "text", "authorId") SELECT "id", "title", "text", "authorId" FROM "post"`);
        await queryRunner.query(`DROP TABLE "post"`);
        await queryRunner.query(`ALTER TABLE "temporary_post" RENAME TO "post"`);
        await queryRunner.query(`CREATE TABLE "temporary_post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" varchar NOT NULL, "authorId" integer NOT NULL, CONSTRAINT "FK_c6fb082a3114f35d0cc27c518e0" FOREIGN KEY ("authorId") REFERENCES "author" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
        await queryRunner.query(`INSERT INTO "temporary_post"("id", "title", "text", "authorId") SELECT "id", "title", "text", "authorId" FROM "post"`);
        await queryRunner.query(`DROP TABLE "post"`);
        await queryRunner.query(`ALTER TABLE "temporary_post" RENAME TO "post"`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "post" RENAME TO "temporary_post"`);
        await queryRunner.query(`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" varchar NOT NULL, "authorId" integer NOT NULL)`);
        await queryRunner.query(`INSERT INTO "post"("id", "title", "text", "authorId") SELECT "id", "title", "text", "authorId" FROM "temporary_post"`);
        await queryRunner.query(`DROP TABLE "temporary_post"`);
        await queryRunner.query(`ALTER TABLE "post" RENAME TO "temporary_post"`);
        await queryRunner.query(`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" varchar NOT NULL, "authorId" integer NOT NULL, CONSTRAINT "FK_14787332409b0000dafe5498d77" FOREIGN KEY ("authorId") REFERENCES "author" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
        await queryRunner.query(`INSERT INTO "post"("id", "title", "text", "authorId") SELECT "id", "title", "text", "authorId" FROM "temporary_post"`);
        await queryRunner.query(`DROP TABLE "temporary_post"`);
    }

}

Steps to Reproduce

I made a sample repository to reproduce this issue.

  1. git clone https://github.com/fossamagna/typeorm-foreign-key-naming-issue-sqlite3.git
  2. Run cd typeorm-foreign-key-naming-issue-sqlite3
  3. Run npm i command
  4. Run npm migration:run command
  5. Run npm migration:check command

My Environment

Dependency Version
Operating System macOS Big Sur
Node.js version v16.14.2
Typescript version 4.5.2
TypeORM version 0.3.7

Additional Context

In typeorm@0.3.6, I got expected behavior.

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 yes
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres no
react-native no
sap no
spanner no
sqlite yes
sqlite-abstract yes
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
fossamagna added a commit to fossamagna/typeorm that referenced this issue Jul 6, 2022
…eck constraint with SQLite

In migration steps, finally temporary table name is renamed to original table name. these constraints name should be created from original table name not temporary table name.

Closes: typeorm#9176
fossamagna added a commit to fossamagna/typeorm that referenced this issue Jul 6, 2022
…eck constraint with SQLite

In migration steps, finally temporary table name is renamed to original table name. these constraints name should be created from original table name not temporary table name.

Closes: typeorm#9176
AlexMesser pushed a commit that referenced this issue Aug 24, 2022
…eck constraint with SQLite (#9185)

* fix: don't use temporary table name to create foreign key, unique, check constraint with SQLite

In migration steps, finally temporary table name is renamed to original table name. these constraints name should be created from original table name not temporary table name.

Closes: #9176

* style: remove unnecessary comment
wirekang pushed a commit to wirekang/typeorm that referenced this issue Aug 25, 2022
…eck constraint with SQLite (typeorm#9185)

* fix: don't use temporary table name to create foreign key, unique, check constraint with SQLite

In migration steps, finally temporary table name is renamed to original table name. these constraints name should be created from original table name not temporary table name.

Closes: typeorm#9176

* style: remove unnecessary comment
nordinh pushed a commit to nordinh/typeorm that referenced this issue Aug 29, 2022
…eck constraint with SQLite (typeorm#9185)

* fix: don't use temporary table name to create foreign key, unique, check constraint with SQLite

In migration steps, finally temporary table name is renamed to original table name. these constraints name should be created from original table name not temporary table name.

Closes: typeorm#9176

* style: remove unnecessary comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant