Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

node.js - Sequelize does not join relations

I make my own kind of shop. In the user's table, you need to make the fields orders, my products and products that the user bought. You need to join 3 tables together (USERS, ORDERS, GOODS). I was good at linking only "my products" to the GOODS table. Namely, when a product is created (added), it is automatically created in the GOODS table and immediately attached to the "my goods" field in the USER table. Now I need to connect 2 fields from the USER table with other 2 fields in the ORDERS table, but either it gives me an error, or the order is created only in the ORDERS table without attaching to the fields in the USERS table. Help me. USERS table:

@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;}

ORDERS table:

@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;}

GOODS table:

@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;}

This is how I create a new product:

 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')
    }

And here's the order:

 //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')
    }
question from:https://stackoverflow.com/questions/65623067/sequelize-does-not-join-relations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay, I figured it out. Suddenly it helps someone: I have incorrectly specified associations in tables. You need to specify the names of the variables. In ORDERS:

@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;

And in USERS:

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


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

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

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...