gpt4 book ai didi

javascript - 使用 TypeORM,向实体添加列时出现 `SQLITE_CONSTRAINT: FOREIGN KEY constraint failed`

转载 作者:行者123 更新时间:2023-12-02 21:32:17 24 4
gpt4 key购买 nike

我使用 TypeORM 作为 TypeScript ORM 库,并使用 SQLite 数据库。

我有一个名为 Photo 的 TypeORM 实体,与另一个名为 PhotoMetadata 的实体具有 @OneToOne 关系。

Photo.ts:

import {
Entity,
Column,
PrimaryGeneratedColumn,
OneToOne,
BaseEntity,
} from 'typeorm';

import PhotoMetadata from './PhotoMetadata';

@Entity()
export default class Photo extends BaseEntity {
@PrimaryGeneratedColumn()
public id: number;

@Column({ length: 100 })
public name: string;

@OneToOne(
() => PhotoMetadata,
(photoMetadata) => photoMetadata.photo,
{ cascade: true },
)
metadata: PhotoMetadata;
}

这是PhotoMetadata.ts:

import {
Entity,
Column,
PrimaryGeneratedColumn,
OneToOne,
JoinColumn,
} from 'typeorm';

import Photo from './Photo';

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

@Column()
comment: string;

@OneToOne(
() => Photo,
(photo) => photo.metadata,
)
@JoinColumn()
photo: Photo;
}

当我向照片添加一列时,例如:

  @Column({ nullable: true })
test: string;

然后运行应用程序,启用日志记录,我得到:

query: BEGIN TRANSACTION
query: SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" IN ('photo_metadata', 'photo', 'user')
query: SELECT * FROM "sqlite_master" WHERE "type" = 'index' AND "tbl_name" IN ('photo_metadata', 'photo', 'user')
query: PRAGMA table_info("user")
query: PRAGMA index_list("user")
query: PRAGMA foreign_key_list("user")
query: PRAGMA table_info("photo")
query: PRAGMA index_list("photo")
query: PRAGMA foreign_key_list("photo")
query: PRAGMA table_info("photo_metadata")
query: PRAGMA index_list("photo_metadata")
query: PRAGMA foreign_key_list("photo_metadata")
query: PRAGMA index_info("sqlite_autoindex_photo_metadata_1")
query: SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" = 'typeorm_metadata'
query: CREATE TABLE "temporary_photo_metadata" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "comment" varchar NOT NULL, "photoId" integer, CONSTRAINT "UQ_99f01ed52303cc16139d69f7464" UNIQUE ("photoId"), CONSTRAINT "FK_99f01ed52303cc16139d69f7464" FOREIGN KEY ("photoId") REFERENCES "photo" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)
query: INSERT INTO "temporary_photo_metadata"("id", "comment", "photoId") SELECT "id", "comment", "photoId" FROM "photo_metadata"
query: DROP TABLE "photo_metadata"
query: ALTER TABLE "temporary_photo_metadata" RENAME TO "photo_metadata"
query: CREATE TABLE "temporary_photo" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(100) NOT NULL)
query: INSERT INTO "temporary_photo"("id", "name") SELECT "id", "name" FROM "photo"
query: DROP TABLE "photo"
query failed: DROP TABLE "photo"
error: [Error: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed] {
errno: 19,
code: 'SQLITE_CONSTRAINT'
}
query: ROLLBACK

如何解决这个问题?由于外键的原因,似乎无法删除我修改的照片表。

最佳答案

我尝试使用 TypeORM 迁移来执行此操作,但遇到了同样的问题。

然后我从 this comment 了解到以下内容:

const connection = await createConnection();

await connection.query('PRAGMA foreign_keys=OFF');
await connection.synchronize();
await connection.query('PRAGMA foreign_keys=ON');

或者,如果您想使用迁移,则从 this comment 开始:

await connection.query("PRAGMA foreign_keys=OFF;");
await connection.runMigrations();
await connection.query("PRAGMA foreign_keys=ON;");

无论哪种情况,您都需要在 ormconfig.json 中设置 synchronize: false

关于javascript - 使用 TypeORM,向实体添加列时出现 `SQLITE_CONSTRAINT: FOREIGN KEY constraint failed`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60591696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com