首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Sequelize -查询对象及其子对象

Sequelize -查询对象及其子对象
EN

Stack Overflow用户
提问于 2019-05-08 12:16:14
回答 2查看 123关注 0票数 2

我正在尝试查询一个模型及其子模型。子级引用父级时使用的是属于。我正在尝试findOne父级并将子级包含在结果中。

以下是我的用户模型:

代码语言:javascript
运行
复制
const Sequelize = require('sequelize');
const connection = require('../middleware/sql-connection');

const User = connection.define('USR', {
    'ID': { type: Sequelize.BIGINT, primaryKey: true },
    'NM': { type: Sequelize.STRING, allowNull: false },
    'EMAIL': { type: Sequelize.STRING, allowNull: false, validate: { isEmail: true } },
    'PHONE': { type: Sequelize.STRING, allowNull: false },
    'STS': { type: Sequelize.BOOLEAN, allowNull: false }
});

module.exports = User;

以下是我的User_TC模型:

代码语言:javascript
运行
复制
const Sequelize = require('sequelize');
const connection = require('../middleware/sql-connection');
const User = require('./user');

const UserTC = connection.define('USR_TC', {
    'USR_ID': { type: Sequelize.BIGINT, primaryKey: true },
    'TC_ACPT': { type: Sequelize.BOOLEAN, allowNull: false }
});

UserTC.belongsTo(User, { as: 'User', foreignKey: 'USR_ID' });
module.exports = UserTC;

下面是我的findOne方法:

代码语言:javascript
运行
复制
User.findOne({
    include: [
        { model: UserTC, as: 'userTc' }
    ],
    where: { ID: req.userData.id }
})
    .then(user => {
        return res.status(200).json(user);
    })
    .catch(err => {
        console.log(err);
        return res.status(500).json({
            message: err
        });
    });

输出抛出了一个错误:SequelizeEagerLoadingError: USR_TC is not associated to USR!

请多多指教!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56033455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档