Javascript Sequelize findOrCreate,包括联营公司;无法读取属性';字段';“未定义”的定义;

Javascript Sequelize findOrCreate,包括联营公司;无法读取属性';字段';“未定义”的定义;,javascript,database,express,orm,sequelize.js,Javascript,Database,Express,Orm,Sequelize.js,您好,我正在使用findOrCreate方法插入记录,但该方法似乎不适用于include associates。如果数据库中存在include关联,则获取错误 使用以下输入 无法读取未定义的属性“字段” 第一次尝试(成功,关系也在“通过”表中创建) 第二次尝试失败(拥有新专辑但作者相同) 错误: “无法读取未定义的属性‘字段’,。。。。。。。“TypeError:不能 读取处未定义的属性“字段” options.defaults.Object.keys.map.name 这是DB函数 var c

您好,我正在使用findOrCreate方法插入记录,但该方法似乎不适用于include associates。如果数据库中存在include关联,则获取错误 使用以下输入

无法读取未定义的属性“字段”

第一次尝试(成功,关系也在“通过”表中创建)

第二次尝试失败(拥有新专辑但作者相同)

错误:

“无法读取未定义的属性‘字段’,。。。。。。。“TypeError:不能 读取处未定义的属性“字段” options.defaults.Object.keys.map.name

这是DB函数

var createAlbum = async (albums) => {
    try {
        // transaction handle
        return await models.sequelize.transaction (t => {

            // Prepare each album to be created
            return Promise.all(albums.map(album => {
                return models.Album.findOrCreate({
                    transaction: t,
                    where: {name: album.name}, // where: {id: album.id}
                    defaults: album,
                    include: ['authors']
                })
            }))
        });

    } catch (error) {
        // transaction will be rolled back if error
        logger.error(error);
        throw error;
    }
}
型号

//album.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    var Album = sequelize.define('Album', {
        id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false },
        name: { type: DataTypes.STRING, primaryKey: true, allowNull: false },
        album_type: { type: DataTypes.STRING, allowNull: false },

    });

    Album.associate = (models) => {
        models.Album.belongsToMany(models.Author, {
            through: 'AlbumAuthor',
            as: 'authors',
            foreignKey: 'album_id',
            otherKey: 'author_name'
        })
    }

    return Album;
}

//author.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    var Author = sequelize.define('Author', {
        name: { type: DataTypes.STRING, primaryKey: true, allowNull: false },
        suffix: { type: DataTypes.STRING, allowNull: true }
    });

    return Author;
}

//album_author.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    var AlbumAuthor = sequelize.define('AlbumAuthor', {
        album_id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false },
        author_name: { type: DataTypes.STRING, primaryKey: true, allowNull: false }
    });

    AlbumAuthor.associate = (models) => {

        models.AlbumAuthor.belongsTo(models.Album, {
            onDelete: 'CASCADE',
            foreignKey: 'album_id',
            targetKey: 'id',
        }),

        models.AlbumAuthor.belongsTo(models.Author, {
            onDelete: 'CASCADE',
            foreignKey: 'author_name',
            targetKey: 'name'
        })
    }

    return AlbumAuthor;
}

嘿,本,我遇到了一个类似的问题,你解决过这个问题吗?@cullanrocks不幸的是,我不得不放弃这个框架。
var createAlbum = async (albums) => {
    try {
        // transaction handle
        return await models.sequelize.transaction (t => {

            // Prepare each album to be created
            return Promise.all(albums.map(album => {
                return models.Album.findOrCreate({
                    transaction: t,
                    where: {name: album.name}, // where: {id: album.id}
                    defaults: album,
                    include: ['authors']
                })
            }))
        });

    } catch (error) {
        // transaction will be rolled back if error
        logger.error(error);
        throw error;
    }
}
//album.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    var Album = sequelize.define('Album', {
        id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false },
        name: { type: DataTypes.STRING, primaryKey: true, allowNull: false },
        album_type: { type: DataTypes.STRING, allowNull: false },

    });

    Album.associate = (models) => {
        models.Album.belongsToMany(models.Author, {
            through: 'AlbumAuthor',
            as: 'authors',
            foreignKey: 'album_id',
            otherKey: 'author_name'
        })
    }

    return Album;
}

//author.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    var Author = sequelize.define('Author', {
        name: { type: DataTypes.STRING, primaryKey: true, allowNull: false },
        suffix: { type: DataTypes.STRING, allowNull: true }
    });

    return Author;
}

//album_author.js
'use strict';
module.exports = (sequelize, DataTypes) => {
    var AlbumAuthor = sequelize.define('AlbumAuthor', {
        album_id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false },
        author_name: { type: DataTypes.STRING, primaryKey: true, allowNull: false }
    });

    AlbumAuthor.associate = (models) => {

        models.AlbumAuthor.belongsTo(models.Album, {
            onDelete: 'CASCADE',
            foreignKey: 'album_id',
            targetKey: 'id',
        }),

        models.AlbumAuthor.belongsTo(models.Author, {
            onDelete: 'CASCADE',
            foreignKey: 'author_name',
            targetKey: 'name'
        })
    }

    return AlbumAuthor;
}