如何在 NestJS 中创建 UUID FK 列?
How can I create a UUID FK column in NestJS?
我 运行 遇到了一个奇怪的问题,我无法在两个实体之间创建 FK 关系。
// organization.entity.ts
@PrimaryGeneratedColumn('uuid')
id: string;
...
@OneToMany(() => User, (user) => user.organization)
users: User[];
// user.entity.ts
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({
type: 'uuid',
})
organizationId: string;
...
@ManyToOne(() => Organization, (organization) => organization.users)
organization: Organization;
在我的 ormconfig.json
文件中,我有这些设置(在连接凭据中)
...
"logging": true,
"entities": [
"dist/**/*.entity{.ts,.js}"
],
"synchronize": true
...
我在 package.json
文件中使用 "typeorm": "^0.2.45"
。
Key columns "organizationId" and "id" are of incompatible types: character varying and uuid.
如何在用户和组织之间建立 FK 关系?
所以根据你的问题我了解到你想要在你的用户中有一个“organizationId”字段 table 这将是一个 FK。
要在组织和用户之间创建一对多关系,请执行以下操作:
// organization.entity.ts
@Entity({ name: 'organizations' })
export class Organization {
@PrimaryGeneratedColumn('uuid')
id: string;
...
@OneToMany(() => User, (user) => user.organization)
users: User[];
}
// user.entity.ts
@Entity({ name: 'users' })
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'uuid' })
organizationId: string;
...
@ManyToOne(() => Organization, (organization) => organization.users)
@JoinColumn({ name: 'organizationId' })
organization: Organization;
}
我 运行 遇到了一个奇怪的问题,我无法在两个实体之间创建 FK 关系。
// organization.entity.ts
@PrimaryGeneratedColumn('uuid')
id: string;
...
@OneToMany(() => User, (user) => user.organization)
users: User[];
// user.entity.ts
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({
type: 'uuid',
})
organizationId: string;
...
@ManyToOne(() => Organization, (organization) => organization.users)
organization: Organization;
在我的 ormconfig.json
文件中,我有这些设置(在连接凭据中)
...
"logging": true,
"entities": [
"dist/**/*.entity{.ts,.js}"
],
"synchronize": true
...
我在 package.json
文件中使用 "typeorm": "^0.2.45"
。
Key columns "organizationId" and "id" are of incompatible types: character varying and uuid.
如何在用户和组织之间建立 FK 关系?
所以根据你的问题我了解到你想要在你的用户中有一个“organizationId”字段 table 这将是一个 FK。
要在组织和用户之间创建一对多关系,请执行以下操作:
// organization.entity.ts
@Entity({ name: 'organizations' })
export class Organization {
@PrimaryGeneratedColumn('uuid')
id: string;
...
@OneToMany(() => User, (user) => user.organization)
users: User[];
}
// user.entity.ts
@Entity({ name: 'users' })
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'uuid' })
organizationId: string;
...
@ManyToOne(() => Organization, (organization) => organization.users)
@JoinColumn({ name: 'organizationId' })
organization: Organization;
}