javascript - 循环内嵌套的 findOrCreate 上的 SequelizeForeignKeyConstraintError

标签 javascript node.js promise sequelize.js

我正在尝试使用 sequelize 和 mysql 在 2 个相关表上插入多行。

基于 sequelize transaction cannot insert because of foreign key? 我尝试创建一个事务,如果我只执行一次 promise 似乎可以工作,但是如果我在显示外键问题的循环上执行代码,它就会失败。

这是代码:

const config = {
    "username": "root",
    "password": "",
    "database": "sequelize_test",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "pool": {
        "max": 1,
        "min": 0,
        "idle": 20000,
        "acquire": 20000
    }
};

const Sequelize = require('sequelize')
const sequelize = new Sequelize(config.database, config.username, config.password, config)

const Project = sequelize.define('projects', {
    id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true
    },
    authorId: {
        type: Sequelize.INTEGER
    },
    name: {
        type: Sequelize.STRING
    }
})

const Author = sequelize.define('authors', {
    id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING,
    }
})

Author.hasMany(Project, {
    foreignKey: 'authorId',
    sourceKey: 'id'
})
Project.belongsTo(Author, {
    foreignKey: 'id'
})

sequelize
    .sync({
        force: true
    })
    .then(() => {

        var projectAuthors = [{
            projectName: "Proj 1",
            authorName: "Author 1"
        }, {
            projectName: "Proj 2",
            authorName: "Author 1"
        }, {
            projectName: "Proj 3",
            authorName: "Author 1"
        }, {
            projectName: "Proj 4",
            authorName: "Author 2"
        }, {
            projectName: "Proj 5",
            authorName: "Author 3"
        }];

        //Insert all the records on the array
        projectAuthors.forEach(function(project) {

            sequelize
                .transaction(function(t) {
                    //First insert the author
                    return Author.findOrCreate({
                        where: {
                            name: project.authorName
                        },
                        transaction: t

                    }).spread(function(author) {
                        //With the id obtained on the previous step, insert the project
                        return Project.findOrCreate({
                            where: {
                                name: project.projectName,
                                authorId: author.id
                            },
                            transaction: t
                        });
                    })

                });

        });
    });

编辑

按照 Ellebkey 的建议,这是我使用 include 的 Controller 。
sequelize
.sync({
    force: true
})
.then(() => {

    var projectAuthors = [{
        projectName: "Proj 1",
        authorName: "Author 1"
    }, {
        projectName: "Proj 2",
        authorName: "Author 1"
    }, {
        projectName: "Proj 3",
        authorName: "Author 1"
    }, {
        projectName: "Proj 4",
        authorName: "Author 2"
    }, {
        projectName: "Proj 5",
        authorName: "Author 3"
    }];

    //Insert all the records on the array
    projectAuthors.forEach(function(project) {

        sequelize
            .transaction(function(t) {
                //First insert the author
                return Project.findOrCreate({
                    where: {
                        name: project.projectName,
                        author: {
                            name: project.authorName
                        }
                    },
                    include: [{
                        association: Project.Author,
                    }],
                    transaction: t
                });

            });
    });
});

它确实失败了:

Invalid value { name: 'Author 3' }



编辑 2(表创建工作)

感谢 Ellebkey 的建议和一些玩弄,我的模型看起来像这样:
const Project = sequelize.define('projects', {
    name: {
        type: Sequelize.STRING
    }
});

const Author = sequelize.define('authors', {
    name: {
        type: Sequelize.STRING,
    }
});

Project.belongsTo(Author, { as: 'Author', foreignKey : 'authorId'});
Author.hasMany(Project, { as: 'Author', foreignKey : 'authorId'});

但是使用答案中的 create() 代码,我仍然得到这个:

Unhandled rejection Error: Invalid value { name: undefined }



顺便说一句,我正在使用 sequelize 4.37.6

提前致谢

最佳答案

好的,首先,根据我的经验,不建议将 foreignKey 用于关联,它会引起混淆。使用 as 代替,而且您不需要创建每个模型的 id,sequelize 为您完成。

const Project = sequelize.define('projects', {
    name: {
        type: Sequelize.STRING
    }
})

现在,回到关联,这将在您的 AuthorId 表上创建一个 Project
Project.belongsTo(Author, { as: 'Author'})

const Author = sequelize.define('authors', {
    name: {
        type: Sequelize.STRING,
    }
})

Author.hasMany(Project, { as: 'Author'})


sequelize
    .sync({
        force: true
    })
    .then(() => {

最后,对于带有 createinclude,您必须根据之前声明的方式创建对象。想想你会用 Find 来做你的 AuthorProjects 所以每个对象应该是这样的:
        var projectAuthors = [{
            name: "Author 1",
            //This must have the same syntax as you declared on your asociation
            Author: [{
                name: "Proj 1"
            },{
                name: "Proj 2"
            }]
        },{
            name: "Author 2",
            Author: [{
                name: "Proj 3"
            },{
                name: "Proj 4"
            }]
        },];

        projectAuthors.forEach(function(project) {

        sequelize
            .transaction(function(t) {
                //First insert the author
                return Author.findOrCreate({
                    where: {
                        //check this I'm not sure
                        name: project.name, //this is the Author name
                    },
                    include: [{
                        model: Project,
                        as: 'Author'
                    }],
                    transaction: t
                });

            });
        });
    });

您遇到的错误是因为您从未在模型上声明 authorName

关于javascript - 循环内嵌套的 findOrCreate 上的 SequelizeForeignKeyConstraintError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49995637/

相关文章:

node.js - Nodejs Expressjs Cluster 集群 app.js 的正确方法

node.js - 如何使用 Bluebird promise AWS SQS

javascript - 可以用生成器函数代替 promise 管理吗?

javascript - CSS3 : translate vs. 翻译X

javascript - 减少返回未定义?

javascript - 使用 Node.js 传输 PCM 音频

node.js - Promise 中的 Nodejs 事件无法解析

php - 网站上的 HTML CSS JavaScript 字体,不使用图像

javascript - 模拟退格键JS

node.js - 获取 SQL 不同数据书架模型