序列化-查询对象及其子对象

时间:2019-05-08 04:16:14

标签: sequelize.js

我正在尝试与子模型一起查询模型。子级引用父级使用的所属。我正在尝试寻找一个父母,并将孩子包括在结果中。

以下是我的用户模型:

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模型:

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方法:

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!

请咨询!

2 个答案:

答案 0 :(得分:0)

您需要

User.hasMany(user_tc)||User.hasOne(User_tc)

用于能够从用户查询usr_tc

答案 1 :(得分:0)

好的,我想出了实现方法。以下是我所做的:

在我的用户模型中:

const Sequelize = require('sequelize');
const connection = require('../middleware/sql-connection');
const UserTC = require('./user-tc');

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 }
});
User.hasOne(UserTC, { as: 'userTc', foreignKey: 'USR_ID' });
module.exports = User;

在我的User_TC模型中:

const Sequelize = require('sequelize');
const connection = require('../middleware/sql-connection');

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

module.exports = UserTC;

和以下findOne方法:

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
        });
    });

像魅力一样工作:) :),以下是我的输出:

{
    "ID": "1",
    "NM": "Tenant-1",
    "EMAIL": "tenant1@gmail.com",
    "PHONE": "1234567890",
    "STS": true,
    "userTc": {
        "USR_ID": "1",
        "TC_ACCPT": false
    }
}