Search code examples
typescriptnestjstypeorm

QueryFailedError upon renaming primary key with TypeORM


I'm building a web app for managing surveys with NestJS and TypeORM.

I use the following two entities with a @OneToMany relation (one survey can have many sections):

survey.entity.ts :

import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { SurveySection } from './surveysection.entity';

@Entity()
export class Survey {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(
    'SurveySection',
    (survey_section: SurveySection) => survey_section.survey,
    {
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
  )
  sections: Array<SurveySection>;
}

and surveysection.entity.ts :

import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Survey } from './survey.entity';

@Entity()
export class SurveySection {
  @PrimaryGeneratedColumn()
  survey_section_ID: number;

  @Column()
  position: number;

  @ManyToOne('Survey', (survey: Survey) => survey.sections)
  @JoinColumn({ name: 'survey_id' })
  survey: Survey;
}

This code runs fine.


But when I rename survey_section_ID in surveysection.entity.ts to anything other than "survey_section_ID" and have synchronize: true in my ormconfig.json I get an error:

QueryFailedError: Incorrect table definition; there can be only one auto column and it must be defined as a key

When I try renaming survey_section_ID to "survey_section_id" (all lowercase), I even get:

QueryFailedError: Duplicate column name 'survey_section_id'

My question is:

Where does the code above rely on the PrimaryGeneratedColumn in surveysection.entity.ts being named "survey_section_ID" and only that?


Edit:

After deleting dist directory from app and readding the exact same entities everything worked.


Solution

  • Update

    After trying out a lot of things, I noticed that for some reason 3 columns of survey_section would keep on being recreated by TypeORM even when not included in the code anymore.
    When deleting surveysection.entity.ts entirely from the project and dropping table survey_section in the database, after restarting the app table survey_section would still appear in the database again with said 3 columns.
    ormlogs.log showed that it removed and readded said 3 columns to table survey_section.
    ormconfig.json had "synchronize": true the whole time.

    After deleting the whole dist directory from the project and readding surveysection.entity.ts the exact same sample code in question worked.

    One of the 3 columns was survey_section_ID. If in fact those 3 columns had always been added behind the scenes, then it would have made sense that no other auto generated column would be addable to the table and that a column named survey_section_id would make for a duplicate name.

    I don't know how to reproduce this or if I just missed something, but I'll leave this as an answer in case anyone is also experiencing this.