多对多 seqelize-typescript 不是唯一约束键问题

Many-to-Many seqelize-typescript not unique constraint key issue

我在通过我的其余 api 服务使用相同的约束键将实体保存在 postgres 数据库中时遇到了一些问题。

我有一个 前提条件 - 实体结构应该如下所示:

正如我正确理解的前提条件,我为客户资源数据中心实现了多对多关联,其中资源是连接点 table many-to-many diagram

当我尝试创建资源时 POST /api/resources- 一切正常。

{"app": "test_app", "customerId": "0001", "datacenterId": "0002"}

但是当我尝试使用同一对密钥

进行下一个POST/api/resources请求时
{"app": "test_app_1",  "customerId": "0001", "datacenterId": "0002" }

我收到 500 错误,因为 Key ("customerId", "datacenterId")=(0001, 0002) 已经存在

所以我的第一个问题实际上是acceptable——在连接点table中存储相同的外键对还是不可能? 如果是的话——我怎样才能达到它? 我真的根据前置条件正确定义了实体间关系的结构吗???

我正在使用 sequlize-typescript 作为 orm 并且有我的代码 ->

客户实体模型:

@Table
export class Customer extends Model {
@Column({
    type: DataType.UUID,
    primaryKey: true,
    defaultValue: DataType.UUIDV4,
})
uuid: string;

@Column({
    type: DataType.STRING,
    allowNull: false,
})
name: string;

@BelongsToMany(() => Datacenter, () => Resource, 'customerId')
datacenters: Datacenter[]

@HasMany(() => Resource)
resources: Resource[]
}

数据中心实体模型:

@Table
export class Datacenter extends Model {
@Column({
    type: DataType.UUID,
    primaryKey: true,
    defaultValue: DataType.UUIDV4,
})
uuid: string;

@Column({
    type: DataType.STRING,
    allowNull: false,
})
name: string;

@BelongsToMany(() => Customer,() => Resource, 'datacenterId')
customers: Customer[]

@HasMany(() => Hardware)
hardware: Hardware[]
}

资源实体模型

@Table
export class Resource extends Model {
@Column({
    type: DataType.UUID,
    primaryKey: true,
    defaultValue: DataType.UUIDV4,
})
uuid: string;

@Column({
    type: DataType.STRING,
    allowNull: false,
})
app_identifier: string;

@ForeignKey(() => Customer)
@Column({
    type: DataType.UUID,
    allowNull: false,
})
customerId: string;

@BelongsTo(() => Customer)
customer: Customer;

@ForeignKey(() => Datacenter)
@Column({
    type: DataType.UUID,
    allowNull: false,
})
datacenterId: string;
}

实体持久化函数:

async create(resource: ResourceDto): Promise<Resource> {
    return await this.resourceRepository.create<Resource>({...resource});
}

我也试过这样做:

    @BelongsToMany(() => Datacenter, {through: {model: Resource, unique: false}})
    datacenters: Datacenter[]

但它甚至无法编译。

您的先决条件不会导致多对多。仅当 Resource 将成为具有独特资源的参考 table 时,您才需要多对多,您可以根据需要向某些客户 link 多次(在这种情况下,您还将需要一个连接 table CustomerResource)。

在您的情况下,您有 客户 资源,每个资源都有 link 到数据中心作为 link 到参考 table具有独特的数据中心记录,可以在不同的资源中多次使用。 因此,您需要与先决条件中声明的完全相同的关系:

User.hasMany(Resource)
Resource.belongsTo(User)
Resource.belongsTo(Datacenter)

也就是说您需要删除两个 @BelongsToMany 定义并使用已经定义的 @BelongsTo@HasMany