🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ### 一对一关联模型 满足条件:一个人只能对应一个身份证,一个身份证只能对应一个人 示例: ![](https://box.kancloud.cn/adddf04317749f8b90e193787062bc43_610x216.png) student 学生表 card 身份证表 >[danger] 一对一可以在任意一个表添加外键 > 注意!!! 一对一外键需要添加 unique 唯一约束 ***** 反向关联 ``` 'use strict'; const moment = require('moment'); module.exports = app => { const { STRING, INTEGER, DATE } = app.Sequelize; const Flow = app.model.define('flow', { id: { type: INTEGER.UNSIGNED, primaryKey: true, autoIncrement: true }, status: { type: INTEGER}, index: { type: INTEGER }, type: { type: INTEGER }, art_id: { type: INTEGER } }); Flow.associate = function() { app.model.Flow.belongsTo(app.model.Movie, { as: 'movie', foreignKey: 'art_id', targetKey: 'id'}); } // 获取最新期刊 Flow.getNewFlowByIndex = async function() { const flow = await this.findOne({ // 主表只查询的字段 attributes: ['index'], include: [{ model: app.model.Movie, as: 'movie', // 副表查询的字段 attributes: ['image', 'content', 'title'] }], order: [ ['index', 'DESC'] ] }); return flow ? flow.dataValues : false; } return Flow; }; ```