gpt4 book ai didi

node.js - Sequelize 不加入关系

转载 作者:行者123 更新时间:2023-12-03 22:32:23 25 4
gpt4 key购买 nike

我自己开店。在用户的表中,您需要制作订单,我的产品和用户购买的产品的字段。您需要将 3 个表连接在一起(用户、订单、商品)。
我擅长只将“我的产品”链接到 GOODS 表。即,当创建(添加)一个产品时,它会自动创建在 GOODS 表中,并立即附加到 USER 表中的“我的 cargo ”字段。现在我需要将 USER 表中的 2 个字段与 ORDERS 表中的其他 2 个字段连接起来,但要么给我一个错误,要么只在 ORDERS 表中创建订单,而没有附加到 USERS 表中的字段。帮我。
用户表:

@Table({tableName:'users'})export class User extends Model<User>{

@PrimaryKey
@Column
id: string;

@Column
username: string;

@Column
email: string;

@Column(DataType.STRING(255))
password: string;

@Column
role: string;

@Column
balance: number;

@HasMany(() => Goods)
myGoods: Goods[]


@HasMany(() => Orders, 'SellerOrders')
orders: Orders[]

@HasMany(()=> Orders, 'BuyerPurchasedGoods')
purchasedGoods: Orders[]



@CreatedAt
@Column
createdAt: Date;

@UpdatedAt
@Column
updatedAt: Date;}
订单表:
@Table({tableName:'orders'}) export class Orders extends Model<Orders>{

@PrimaryKey
@Column
id: string;

@AllowNull(false)
@Column(DataType.JSON)
info: any;

@AllowNull(false)
@Column(DataType.STRING)
state: string;

//USER
@ForeignKey(() => User)
@Column({
type: DataType.STRING,
allowNull: false,
})
ownerId: any;

@BelongsTo(() => User, 'SellerOrders')
owner: User;

@ForeignKey(() => User)
@Column({
type: DataType.STRING,
allowNull: false,
})
buyerId: any;

@BelongsTo(() => User, "BuyerPurchasedGoods")
buyer: User;

//DATE
@CreatedAt
@Column
createdAt: Date;

@UpdatedAt
@Column
updatedAt: Date;}
商品表:
@Table({tableName:'goods'}) export class Goods extends Model<Goods>{


@PrimaryKey
@Column
id: string;

@AllowNull(false)
@Column
title: string;

@AllowNull(false)
@Column
category: string;

@AllowNull(false)
@Column(DataType.JSON)
info: any

@AllowNull(false)
@Column
price: number;

@AllowNull(false)
@Column
amount: number;

@BelongsTo(() => User)
user: User;

@ForeignKey(() => User)
@Column({
type: DataType.STRING,
allowNull: false,
})
userId: string;

@CreatedAt
@Column
createdAt: Date;

@UpdatedAt
@Column
updatedAt: Date;}
这就是我创建新产品的方式:
 try {
const id = shortid.generate()
await this.goodsModel.create({ ...createItemDto, id, userId: user.id})
return 'OK'
} catch (error) {
throw new InternalServerErrorException('Failure to create new item')
}
这是顺序:
 //generate order
try {
const id = shortid.generate();
await this.ordersModel.create({
id,
ownerId: item.userId,
buyerId: userObj.id,
info:{ ...generateOrderDto },
state:'processing'
},
{include:[{all:true}]}
)
return 'OK'
} catch (error) {
console.log(error)
throw new InternalServerErrorException('Cannot generate order')
}

最佳答案

好吧,我想通了。突然它帮助了某人:
我在表中错误地指定了关联。您需要指定变量的名称。
在订单中:

@ForeignKey(() => User)
@Column({
type: DataType.STRING,
allowNull: false,
})
ownerId: any;

@BelongsTo(() => User, 'ownerId')
owner: User;

@ForeignKey(() => User)
@Column({
type: DataType.STRING,
allowNull: false,
})
buyerId: any;

@BelongsTo(() => User, 'buyerId')
buyer: User;
在用户中:
    @HasMany(() => Goods)
myGoods: Goods[]


@HasMany(() => Orders, 'ownerId')
orders: Orders[]

@HasMany(()=> Orders, 'buyerId')
purchasedGoods: Orders[]
祝你好运!

关于node.js - Sequelize 不加入关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65623067/

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