续集多对多联合连接表不填充

时间:2016-03-01 03:50:33

标签: mysql node.js sequelize.js

我在两个模型HospitalContact之间存在多对多关联。已添加联系人记录,但关联表hospitals_contacts中没有条目。 sequelize生成外键contact_id和hospital_id作为复合主键。

联系模式

module.exports = function(sequelize, DataTypes) {
    var Contact = sequelize.define("Contact", {

        job_title: DataTypes.STRING,
        first_name: DataTypes.STRING,
        last_name: DataTypes.STRING, 
        archived: DataTypes.BOOLEAN,
        primary_email: {

            type: DataTypes.STRING,
            validate: {
                isEmail: true
            }
        },
        business_number: DataTypes.STRING,
        number_extension: DataTypes.STRING,
        fax_number: DataTypes.STRING

    }, {

        underscored:'true',
        tableName: 'contacts',
        createdAt: false,
        updatedAt: false,

        classMethods: {
            associate: function(models) {
                Contact.belongsToMany(models.Hospital, { through: 'hospitals_contacts'});    
                Contact.belongsTo(models.Type);
                Contact.belongsTo(models.Region)
            }
        },
    });



    return Contact;
};

医院模型

module.exports = function(sequelize, DataTypes) {

    var Hospital = sequelize.define("Hospital", {

        name: DataTypes.STRING,
        mak_code: DataTypes.STRING,
        licensed_tm_lab: DataTypes.BOOLEAN

    }, { 

        underscored:'true',
        tableName: 'hospitals',     
        createdAt: false,
        updatedAt: false,

        classMethods: {
            associate: function(models) {

                Hospital.belongsToMany(models.Contact, { through: 'HospitalsContacts'});
                Hospital.belongsTo(models.Address)
            }
        }
    }); 

    return Hospital;
};
用于添加联系人的

api端点

var hospitalInstance = models.Hospital;
var contactData = req.body;

models.Contact.create({ 
    first_name: contactData.first_name, 
    last_name: contactData.last_name, 
    type_id: contactData.type,
    job_title: contactData.job_title, 
    primary_email: contactData.email, 
    business_number:contactData.phone_number,
    fax_number:contactData.fax,
    number_extension:contactData.extension,
    region_id: contactData.region,
    committee_id: contactData.committee,
    archived: false,
    hospitalInstance: {id:contactData.hospital}
    },
    {
        include: [hospitalInstance]

    });

1 个答案:

答案 0 :(得分:0)

我建议您尝试以下方法:

更改您的"至" Contact中的属性与Hospital:HospitalsContacts中的属性相匹配,因此您拥有:

Contact.belongsToMany(models.Hospital, { through: 'HospitalsContacts'}); //Contact
Hospital.belongsToMany(models.Contact, { through: 'HospitalsContacts'}); //Hospital

现在我假设您已经创建了医院,并且只想创建一个与该医院相关的新联系人,这是正确的吗?如果是这样的话:

var hospital = (...)

hospital.createHospitalContact({
   first_name: contactData.first_name, 
   last_name: contactData.last_name, 
   type_id: contactData.type,
   job_title: contactData.job_title, 
   primary_email: contactData.email, 
   business_number:contactData.phone_number,
   fax_number:contactData.fax,
   number_extension:contactData.extension,
   region_id: contactData.region,
   committee_id: contactData.committee,
   archived: false,
}).then(function(contact) {
   //Do something
})