Sequelize创建包括

时间:2015-03-25 12:45:36

标签: javascript mysql node.js sequelize.js

我有以下模型和分析:

    var AcademyModule = sequelize.define('academy_module', {
        academy_id:
        {
            type: DataTypes.INTEGER,
            primaryKey: true
        },
        module_id:
        {
            type: DataTypes.INTEGER,
            primaryKey: true
        },
        module_module_type_id: DataTypes.INTEGER,
        sort_number: DataTypes.INTEGER,
        requirements_id: DataTypes.INTEGER
    }, {
        freezeTableName: true,
        instanceMethods: {
            retrieveAll: function (academy_id,onSuccess, onError) {
                AcademyModule.find({include: [{all: true}],where: {academy_id: academy_id}}, {})
                    .success(onSuccess).error(onError);
            },
            retrieveById: function (academy_id, onSuccess, onError) {
                AcademyModule.findAll({include: [{all: true}],where: {academy_id: academy_id}}, {})
                    .success(onSuccess).error(onError);
            },
            add: function (requirements,academyModule,onSuccess, onError) {
                var academyModule = academyModule;
                if(requirements == null)
                {
                    AcademyModule.build(this.dataValues)
                        .save().ok(onSuccess).error(onError);
                }
                else
                {
                    var am = {academy_id: academyModule.academy_id, module_id: academyModule.module_id, sort_number: academyModule.sort_number}
                    AcademyModule.build(am);
                    requirements.academy_module = am;

                    Requirements.create(requirements, {
                        include: [AcademyModule]
                    });
                }
            },
            updateById: function (quote_id, onSuccess, onError) {
                var id = quote_id;
                var quotes = this.quotes;

                AcademyModule.update({quotes: quotes}, {where: {id: id}})
                    .success(onSuccess).error(onError);
            },
            removeById: function (module_id,academy_id, onSuccess, onError) {
                AcademyModule.destroy({where: {academy_id: academy_id, module_id: module_id}}).success(onSuccess).error(onError);
            }
        }
    }
),
 Module = sequelize.define('module', {
    id: DataTypes.INTEGER,
    name: DataTypes.STRING,
    description: DataTypes.STRING,
    category_id: DataTypes.STRING,
    module_type_id: DataTypes.STRING,
    gives_score: DataTypes.INTEGER,
    duration: DataTypes.STRING,
    price: DataTypes.STRING

},{freezeTableName:true}),
    Academy = sequelize.define('academy', {
        id: DataTypes.INTEGER,
        name: DataTypes.STRING,
        created: DataTypes.DATE,
        description: DataTypes.STRING,
        rating: DataTypes.STRING,
        status_id: DataTypes.INTEGER,
        organization_id: DataTypes.INTEGER,
        division_id: DataTypes.INTEGER,
        icon: DataTypes.STRING

    }, {
        freezeTableName: true}),
    Requirements = sequelize.define('requirements', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
    value: DataTypes.STRING,
    requirement_type_id: DataTypes.INTEGER
}, {
    freezeTableName: true}),
    ModuleType = sequelize.define('module_type', {
        id: DataTypes.INTEGER,
        name: DataTypes.STRING,
        color: DataTypes.STRING,
        icon: DataTypes.STRING,
        type_description: DataTypes.STRING

    },{freezeTableName:true});

AcademyModule.belongsTo(Module, {foreignKey: 'module_id'});
AcademyModule.belongsTo(Academy, {foreignKey: 'academy_id'});
AcademyModule.belongsTo(Requirements, {foreignKey: 'requirements_id'});
AcademyModule.belongsTo(ModuleType, {foreignKey:'module_module_type_id'});
Requirements.hasOne(AcademyModule, {foreignKey: 'requirements_id'});

现在你可以看到我和AcademyModule之间有关系。 Requirements

事情是必须在requirements看到之前创建academyModule行,因为必须填充AcademyModule requirements_id

为了解决这个问题,我创建了以下添加方法:

    var academyModule = academyModule;
if(requirements == null)
{
    AcademyModule.build(this.dataValues)
        .save().ok(onSuccess).error(onError);
}
else
{
    var am = {academy_id: academyModule.academy_id, module_id: academyModule.module_id, sort_number: academyModule.sort_number}
    AcademyModule.build(am);
    requirements.academy_module = am;

    Requirements.create(requirements, {
        include: [AcademyModule]
    });
}

但是在这里它只在requirements表中插入一行而不在academy_module表中。

所以我的问题是可能出了什么问题?以及如何确保在两个表中插入?

0 个答案:

没有答案