首页 > 其他分享 >Sequelize的聚合查询查出messsage表中userId字段再user表中的用户信息

Sequelize的聚合查询查出messsage表中userId字段再user表中的用户信息

时间:2023-12-05 18:46:00浏览次数:31  
标签:userId messsage modelRelation User 表中 import Message Sequelize

这里记个一对多的,其他类似

模型正常写,然后:

入口文件main.js

// ...
import modelRelation from "./services/modelRelation";
// ...


// 同步模型和数据库
modelRelation();

modelRelation.js

// import { Sequelize } from "sequelize";
import sequelize from "./pool";

import Message from "../models/Message";
import User from "../models/User";


export default function(){
    User.hasMany(Message, {foreignKey: 'from' });
    Message.belongsTo(User, {foreignKey: 'from', targetKey: 'id', as: 'from_user', constraints: false});

    sequelize.sync();
}

 

标签:userId,messsage,modelRelation,User,表中,import,Message,Sequelize
From: https://www.cnblogs.com/facingscreen/p/17877893.html

相关文章