As a newcomer to typeorm, I've encountered a peculiar issue with the synchronize: true
setting in my ormconfig.js
. Whenever I make changes to an entity with a Many-to-Many relationship, any data present in the join table prior to the entity modification disappears.
Let's take a look at my Project
Entity:
@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[];
}
Now, let's examine my Group
Entity:
@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[];
}
If I introduce a new field to either the Group or the Project, an automatic migration takes place running the following queries:
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"
Shouldn't the relationship table retain the existing data?