关联协会。基于与另一个模型中的主键相同的字段,两个文件作为模型中的外键

时间:2019-01-31 13:05:08

标签: mysql node.js sequelize.js sequelize-typescript

我有两个模型。一个叫Webusers,另一个叫SalesContact。 Web用户有两个字段作为外键,它们指向SalesContact Model中的同一字段。

这是我的代码:

// SalesContact Model file
const salesContact = sequelize.define('sales_contact', {
    id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
    email: { type: Sequelize.STRING, unique: true, allowNull: false },
    role: { type: Sequelize.ENUM('commercial', 'avant-vente') },
    firstName: { type: Sequelize.STRING, field: 'first_name', allowNull: false },
    lastName: { type: Sequelize.STRING, field: 'last_name', allowNull: false }
}, {
    defaultScope: {
        attributes: { exclude: ['created_at', 'updated_at'] }
    },
    underscored: true,
    paranoid: false,
    freezeTableName: true
});

//Webusers Model file
const SalesContact = require('./sales-contact');

const Webusers = sequelize.define('webusers', {

    ID: { type: Sequelize.INTEGER(8), primaryKey: true, autoIncrement: true },
    firstname: Sequelize.STRING(50),
    lastname: Sequelize.STRING(40),
    login: Sequelize.STRING(40),
    firm: Sequelize.STRING(40),
    associated_presale_id: Sequelize.INTEGER(11),
    account_manager_id: Sequelize.INTEGER(11),
    ref: Sequelize.INTEGER,
    amount: Sequelize.STRING(15),
    cg:  {
        type: Sequelize.SMALLINT,
        get() {
            let ve_abonne = this.getDataValue('cg');
            if (!_.isNull(ve_abonne)) {
                ve_abonne = (ve_abonne === 1) ? true : false;
            }
            return ve_abonne;
        },
        set(val) {
            val = (val === true) ? 1 : 0;
            this.setDataValue(val);
        }
    },
    cp:  {
        type: Sequelize.SMALLINT,
        get() {
            let ve_abonne = this.getDataValue('cp');
            if (!_.isNull(ve_abonne)) {
                ve_abonne = (ve_abonne === 1) ? true : false;
            }
            return ve_abonne;
        },
        set(val) {
            val = (val === true) ? 1 : 0;
            this.setDataValue(val);
        }
    },

},getterMethods: {
        clientInformation() {
            const client = _.pickBy({
                id: this.ID,
                firstName: this.firstname,
                lastName: this.lastname,
                login: this.login,
                companyName: this.firm,
            }, _.identity);

            const salesDetails = _.pickBy({
                subscriberId: this.ref,
                amount: this.amount,
                generalTerms: this.cg,
                specificTerms: this.cp
            }, _.identity);

          Webusers.belongsTo(SalesContact, { as: 'AssociatedPresale', foreignKey: 'associated_presale_id', targetKey: 'id'});
          Webusers.belongsTo(SalesContact, { as: 'AccountManager', foreignKey: 'account_manager_id', targetKey: 'id' });
            Webusers.findByPk(this.ID, {
               include: [{
                  model: SalesContact,
                  as: 'AccountManager'
               }, {
                  model: SalesContact,
                  as: 'AssociatedPresale'
               } ]
            }).then(function(result){

              if (!_.isEmpty(result.AssociatedPresale.dataValues)) {
                 salesDetails.associatedPresale = result.AssociatedPresale.dataValues;
             }

             if (!_.isEmpty(result.AccountManager.dataValues)) {
                salesDetails.accountManager = result.AccountManager.dataValues;
            }
             if (!_.isEmpty(salesDetails)) {
                 client.salesDetails = salesDetails;
             }

           });

            return client;
        }
    }
});

当我尝试调用 Webusers.get('clientInformation')函数时,出现以下错误: SequelizeAssociationError:您在两个单独的关联中使用了别名AssociatedPresale。别名关联必须具有唯一的别名

请给我一些帮助吗?

0 个答案:

没有答案