这里记个一对多的,其他类似
模型正常写,然后:
入口文件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