【问题标题】:Sequelize insert row many to many relationship续集插入行多对多关系
【发布时间】:2019-09-05 05:44:22
【问题描述】:

我是使用 Node.js 进行 Sequelize 的新手。我试图将数据插入到我的多对多关系中。根据文档,我们在创建主对象后设置了可用的 [modelName] 函数。然而,这对我来说似乎并非如此。如果有人能指出我正确的方向,我将不胜感激。干杯!

我的学生模型:

const Sequelize = require('sequelize');
const sequelize = require('../db/database');

const Students = sequelize.define('student', {
    studentId: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true
    },

    firstName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    middleName: {
        type: Sequelize.STRING,
        allowNull: true
    },
    lastName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    dateOfBirth: {
        type: Sequelize.STRING,
        allowNull: false
    },
    gender: {
        type: Sequelize.STRING,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING,
        allowNull: true
    },
    phoneNumber: {
        type: Sequelize.STRING,
        allowNull: true
    }
});

module.exports = Students;

我的联系方式:

const Sequelize = require('sequelize');
const sequelize = require('../db/database');

const Contacts = sequelize.define('contact', {
    contactId: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true
    },
    firstName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    middleName: {
        type: Sequelize.STRING,
        allowNull: true
    },
    lastName: {
        type: Sequelize.STRING,
        allowNull: false
    },
    dateOfBirth: {
        type: Sequelize.STRING,
        allowNull: false
    },
    gender: {
        type: Sequelize.STRING,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING,
        allowNull: true
    },
    phoneNumber: {
        type: Sequelize.STRING,
        allowNull: true
    }
});

module.exports = Contacts;

我的关系:

Student.belongsToMany(Contact, {
    through: 'StudentContacts',
    timestamps: false
});

Contact.belongsToMany(Student, {
    through: 'StudentContacts',
    timestamps: false
});

我的数据库图:

我保存对象的代码

student = new Student({
            firstName,
            middleName,
            lastName,
            dateOfBirth,
            gender,
            email,
            phoneNumber,
            classId,
            tenantId: req.user.tid
        });

        // create student
        const newStudent = await student.save();
        newStudent
            .setContact({
                firstName: contact.firstName,
                lastName: contact.lastName,
                middleName: contact.middleName,
                dateOfBirth: contact.dateOfBirth,
                gender: contact.gender,
                email: contact.email,
                phoneNumber: contact.phoneNumber
            })
            .save();
        console.log('student', newStudent);

我的邮递员创建:

更新代码以反映建议的答案

Student.create(
            {
                firstName,
                middleName,
                lastName,
                dateOfBirth,
                gender,
                email,
                phoneNumber,
                classId,
                tenantId: req.user.tid,
                contact: [
                    {
                        firstName: contact.firstName,
                        lastName: contact.lastName,
                        middleName: contact.middleName,
                        dateOfBirth: contact.dateOfBirth,
                        gender: contact.gender,
                        email: contact.email,
                        phoneNumber: contact.phoneNumber
                    }
                ]
            },
            {
                include: [Contact]
            }
        );

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    让我们介绍将产品与许多标签相关联的功能。设置模型可能如下所示:

    class Tag extends Model {}
    Tag.init({
      name: Sequelize.STRING
    }, { sequelize, modelName: 'tag' });
    
    Product.hasMany(Tag);
    // Also works for `belongsToMany`.
    Now we can create a product with multiple tags in the following way:
    

    代码使用示例

    Product.create({
      id: 1,
      title: 'Chair',
      tags: [
        { name: 'Alpha'},
        { name: 'Beta'}
      ]
    }, {
      include: [ Tag ]
    })
    

    这个例子是 hasMany 但它是一样的 belongsToMany

    【讨论】:

    • 我试过这个,看看它是否适用于多对多,但似乎不行。它只执行: : INSERT INTO [students] ([firstName],[middleName],[lastName],[dateOfBirth],[gender],[createdAt],[updatedAt],[classId],[tenantId]) 输出插入。 * 值 (@0,@1,@2,@3,@4,@5,@6,@7,@8);有关添加的代码,请参阅上面的编辑帖子。
    猜你喜欢
    • 2015-05-16
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2016-03-31
    • 1970-01-01
    相关资源
    最近更新 更多