我在我的本机项目中使用Typeorm,我有3个实体,这是我的连接:
connectDb() {
return createConnection(
{
type: "react-native",
database: "Beita",
location: "default",
logging: ["error","query"],
synchronize: true,
entities: [
CategoryEntity,
PoemEntity,
HemistichEntity,
]
}
);
}
如您所知,将synchronize
设置为true
时,您的实体将在每次运行应用程序时与数据库同步。
为此TypeOrm将删除每个表并重新创建它们。
但是在向表中添加一些数据并重新运行应用程序后,我得到了此错误:
console.error: "query failed: ", "DROP TABLE "PoemEntity""
这些是我的实体:
@Entity("PoemEntity")
export class PoemEntity {
...
@OneToMany(
type => HemistichEntity,
hemistichEntity => hemistichEntity.poem,
)
hemistichs!: HemistichEntity[];
@ManyToOne(
type => CategoryEntity,
categoryEntity => categoryEntity.poems
)
category!: CategoryEntity;
}
@Entity("HemistichEntity")
export class HemistichEntity {
...
@ManyToOne(
type => PoemEntity,
poemEntity => poemEntity.hemistichs
)
poem!: PoemEntity;
}
@Entity("CategoryEntity")
export class CategoryEntity {
...
@OneToMany(
type => PoemEntity,
poemEntity => poemEntity.category
)
poems!: PoemEntity[];
}