ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[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; }; ```