联合查询时 var User = sequelize.define(‘user’, {/* … /}) var Project = sequelize.define(‘project’, {/ … */})
// hasOne 关系
Project.hasOne(User)
这个表示了1:1的关系
我在使用egg.js时,我该去哪里设置这个关系?
\app\model\user.js
module.exports = app => { const {STRING} = app.Sequelize const User = app.model.define(‘user’, {/* … */}) return User }
\app\model\teacher.js
module.exports = app => { const {STRING} = app.Sequelize const Teacher = app.model.define(‘teacher’, {/* … */}) return Teacher }
\app.js
module.exports = app => { app.beforeStart(async () => { app.model.Teacher.hasOne(app.model.User) app.model.User.belongsTo(app.model.Teacher) await app.model.sync({‘force’: true}) }) }
在对应的 model
里面做
@atzcl 在egg.js中如果在对应的model里做,在先定义的model中会出现找不到后定义的model