【发布时间】:2021-06-20 13:49:32
【问题描述】:
我是 typeorm 的新手,但我发现 synchronize: true 功能存在一个非常奇怪的问题,该功能设置在我的 ormconfig.js 中。当我修改具有多对多关系的实体时,在进行实体更改之前连接表中的任何数据都会消失。
这是我的Project 实体
@ObjectType()
@Entity("project")
export class Project extends BaseEntity {
@Field(() => Int)
@PrimaryColumn()
id: number;
@Field()
@Column("text")
name: string;
@Field({ nullable: true })
@Column("text", { nullable: true })
cluster?: string;
//project_groups
@ManyToMany((type) => Group, (group) => group.projects, { lazy: true })
@Field((type) => [Group], { nullable: true })
@JoinTable()
groups?: Group[];
}
这是我的Group 实体
@ObjectType()
@Entity("group")
export class Group extends BaseEntity {
@Field(() => String)
@PrimaryColumn()
id: string;
@Field()
@Column("text")
name: string;
@Field({ nullable: true })
@Column("text")
type: string;
@Field({ nullable: true })
@Column("text", { nullable: true })
currency: string;
@Field((type) => [Project])
@ManyToMany((type) => Project, (project) => project.groups, { lazy: true })
projects: Project[];
//group_modifiers
@ManyToMany((type) => Modifier, (modifier) => modifier.group, { lazy: true })
@Field((type) => [Modifier])
@JoinTable()
modifiers: Modifier[];
}
如果我向 Group 或 Project 添加一个新字段,迁移会自动运行,并且会出现以下查询
query: CREATE TABLE "temporary_project" ("id" integer PRIMARY KEY NOT NULL, "name" text NOT NULL, "cluster" text, "asdfa" text)
query: INSERT INTO "temporary_project"("id", "name", "cluster") SELECT "id", "name", "cluster" FROM "project"
query: DROP TABLE "project"
query: ALTER TABLE "temporary_project" RENAME TO "project"
关系表不应该持久化数据吗?
【问题讨论】:
标签: typescript sqlite graphql typeorm