【发布时间】:2021-07-16 14:35:28
【问题描述】:
我正在构建一个 web 应用程序来管理带有 NestJS 和 TypeORM 的调查。
我使用以下两个具有@OneToMany 关系的实体(一个调查可以有多个部分):
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>;
}
和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;
}
这段代码运行良好。
但是,当我将 surveysection.entity.ts 中的 survey_section_ID 重命名为“survey_section_ID”以外的任何名称并在我的 ormconfig.json 中添加 synchronize: true 时,我收到错误消息:
QueryFailedError: Incorrect table definition; there can be only one auto column and it must be defined as a key
当我尝试将 survey_section_ID 重命名为“survey_section_id”(全部小写)时,我什至得到:
QueryFailedError: Duplicate column name 'survey_section_id'
我的问题是:
上面的代码在哪里依赖于surveysection.entity.ts 中的PrimaryGeneratedColumn 被命名为“survey_section_ID”,仅此而已?
编辑:
从应用程序中删除dist 目录并读取完全相同的实体后,一切正常。
【问题讨论】:
-
感谢您的资源。还在为这个苦苦挣扎。阅读文档并更多地使用代码。当我删除
surveysection.entity.ts中的@JoinColumn()并重命名PrimaryGeneratedColumn时,它会抛出QueryFailedError: You can't delete all columns with ALTER TABLE; use DROP TABLE instead。然后我必须手动删除表survey_section并反向重命名PrimaryColumn以便再次连接到数据库。由于某种原因,相同的设置在section_question没有QueryFailedError的桌子上完美运行
标签: typescript nestjs typeorm