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

Why is second migration generated? #9229

Closed
Dmitriyx opened this issue Jul 21, 2022 · 1 comment · Fixed by #9677
Closed

Why is second migration generated? #9229

Dmitriyx opened this issue Jul 21, 2022 · 1 comment · Fixed by #9677

Comments

@Dmitriyx
Copy link

Dmitriyx commented Jul 21, 2022

Issue Description

For some reason, TypeORM generates a second migration file after I run all migrations.

Expected Behavior

Typeorm would not generate second migration file.

Actual Behavior

Typeorm is generating a second migration file

Model Used

import { Column, Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm';
@Entity('testEntity')
export class TestEntity {
    @PrimaryColumn()
    @OneToOne('User', 'id')
    @JoinColumn({ name: 'userId' })
    public userId: string;

    @Column({ type: 'jsonb', nullable: false })
    public x: string[];
}

First Generated Migration File

import {MigrationInterface, QueryRunner} from "typeorm";

export class test1658425247239 implements MigrationInterface {
    name = 'test1658425247239'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            CREATE TABLE "testEntity" (
                "userId" uuid NOT NULL,
                "x" jsonb NOT NULL,
                CONSTRAINT "REL_3138077fc5fcc8a4e4e3c175f4" UNIQUE ("userId"),
                CONSTRAINT "PK_3138077fc5fcc8a4e4e3c175f40" PRIMARY KEY ("userId")
            )
        `);
        await queryRunner.query(`
            ALTER TABLE "testEntity"
            ADD CONSTRAINT "FK_3138077fc5fcc8a4e4e3c175f40" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
        `);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE "testEntity" DROP CONSTRAINT "FK_3138077fc5fcc8a4e4e3c175f40"
        `);
        await queryRunner.query(`
            DROP TABLE "testEntity"
        `);
    }

}

Second Generated Migration File - Why ??

import {MigrationInterface, QueryRunner} from "typeorm";

export class test21658425259126 implements MigrationInterface {
    name = 'test21658425259126'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE "testEntity" DROP CONSTRAINT "FK_3138077fc5fcc8a4e4e3c175f40"
        `);
        await queryRunner.query(`
            ALTER TABLE "testEntity"
            ADD CONSTRAINT "UQ_3138077fc5fcc8a4e4e3c175f40" UNIQUE ("userId")
        `);
        await queryRunner.query(`
            ALTER TABLE "testEntity"
            ADD CONSTRAINT "FK_3138077fc5fcc8a4e4e3c175f40" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
        `);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE "testEntity" DROP CONSTRAINT "FK_3138077fc5fcc8a4e4e3c175f40"
        `);
        await queryRunner.query(`
            ALTER TABLE "testEntity" DROP CONSTRAINT "UQ_3138077fc5fcc8a4e4e3c175f40"
        `);
        await queryRunner.query(`
            ALTER TABLE "testEntity"
            ADD CONSTRAINT "FK_3138077fc5fcc8a4e4e3c175f40" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION
        `);
    }

}

Steps to Reproduce

  1. Generate Migration
  2. Run migrations
  3. Generate Migration (expect not to generate, actual: generates new migration)

My Environment

Dependency Version
Operating System Mac M1
Node.js version 16.15
Typescript version 4.4.4
TypeORM version 0.2.38
TypeDi version 0.8.0
typeorm-typedi-extensions 0.2.3

Additional Context

Here are the list of packages we are using

"dependencies": {
    "@sentry/node": "^6.17.7",
    "@sentry/tracing": "^6.17.7",
    "@types/pg": "^8.6.5",
    "algoliasearch": "^4.12.2",
    "analytics-node": "^5.0.0",
    "aws-sdk": "^2.1058.0",
    "axios": "^0.21.1",
    "bcrypt": "^5.0.1",
    "bull": "^4.1.0",
    "chalk": "^2.4.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.1",
    "class-validator-jsonschema": "^3.0.1",
    "compression": "^1.7.1",
    "copyfiles": "^2.1.0",
    "cors": "^2.8.4",
    "dotenv": "6.0.0",
    "eslint-plugin-eslint-comments": "^3.2.0",
    "express": "^4.16.2",
    "express-basic-auth": "^1.1.3",
    "express-rate-limit": "^5.5.1",
    "faker": "^5.5.3",
    "ffmpeg-static": "^5.0.0",
    "figlet": "^1.2.0",
    "glob": "^7.1.2",
    "helmet": "^4.4.1",
    "ioredis": "^4.28.3",
    "jsonfile": "5.0.0",
    "jsonwebtoken": "^8.5.1",
    "microframework-w3tec": "^0.6.3",
    "moment": "^2.29.1",
    "morgan": "^1.9.0",
    "multer": "^1.4.2",
    "nocache": "^2.1.0",
    "node-redshift": "^0.1.5",
    "nodemon": "^2.0.13",
    "nps": "^5.10.0",
    "nps-utils": "^1.7.0",
    "opentok": "^2.12.1",
    "pg": "^8.7.3",
    "rate-limit-redis": "^2.1.0",
    "reflect-metadata": "^0.1.13",
    "routing-controllers": "^0.9.0",
    "routing-controllers-openapi": "^3.1.0",
    "sentry-testkit": "^3.3.7",
    "serve-favicon": "^2.4.5",
    "simple-thumbnail": "^1.6.5",
    "stream-chat": "^6.5.1",
    "stripe": "^8.197.0",
    "supertest": "^3.0.0",
    "swagger-ui-express": "^4.3.0",
    "ts-node": "7.0.1",
    "twilio": "^3.73.0",
    "typedi": "0.8.0",
    "typeorm": "^0.2.34",
    "typeorm-seeding": "^1.6.1",
    "typeorm-typedi-extensions": "^0.2.3",
    "typescript": "^4.2.3",
    "uuid": "^8.3.2",
    "winston": "3.1.0"
  }

Relevant Database Driver(s)

DB Type Reproducible
postgres yes

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.
@pleerock
Copy link
Member

Use case is not valid. Relational decorators must be applied to the properties with the type of the object to which relation maps, e.g.:

import { Column, Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm';
@Entity('testEntity')
export class TestEntity {
    @PrimaryColumn()
    public userId: string;

    @OneToOne('User', 'test')
    @JoinColumn({ name: 'userId' })
    public user: User;

    @Column({ type: 'jsonb', nullable: false })
    public x: string[];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants