Gar*_*ary 3 javascript sqlite orm typescript typeorm
我使用 TypeORM 作为 TypeScript ORM 库,带有 SQLite 数据库。
我有一个 TypeORM 实体,它与另一个实体Photo有@OneToOne关系,称为PhotoMetadata.
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;
}
Run Code Online (Sandbox Code Playgroud)
这里是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;
}
Run Code Online (Sandbox Code Playgroud)
当我向 中添加一列时Photo,例如:
@Column({ nullable: true })
test: string;
Run Code Online (Sandbox Code Playgroud)
然后运行应用程序,启用日志记录,我得到:
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
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?由于外键,删除我修改的 Photo 表似乎失败。
我尝试使用 TypeORM 迁移来做到这一点,但我遇到了同样的问题。
然后我从这个评论中了解到以下内容:
const connection = await createConnection();
await connection.query('PRAGMA foreign_keys=OFF');
await connection.synchronize();
await connection.query('PRAGMA foreign_keys=ON');
Run Code Online (Sandbox Code Playgroud)
或者,如果您想改用迁移,请从此评论:
await connection.query("PRAGMA foreign_keys=OFF;");
await connection.runMigrations();
await connection.query("PRAGMA foreign_keys=ON;");
Run Code Online (Sandbox Code Playgroud)
无论哪种情况,您都需要synchronize: false在ormconfig.json.
| 归档时间: |
|
| 查看次数: |
4027 次 |
| 最近记录: |