title: SequelizeForeignKeyConstraintError on nested findOrCreate inside a loop
body: <p>I am trying to insert multiple rows on 2 related tables using sequelize with mysql.</p>
  
  <p>Based on <a href="https://stackoverflow.com/questions/45615767/sequelize-transaction-cannot-insert-because-of-foreign-key">sequelize transaction cannot insert because of foreign key?</a> I tried to create a transaction and it seems to work if I execute the promise only once, but it fails if I execute the code on a loop showing a foreign key issue.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>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(() =&gt; {
  
          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
                          });
                      })
  
                  });
  
          });
      });
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>Following the suggestion from  Ellebkey, here is my controller using include.</p>
  
  <pre><code>sequelize
  .sync({
      force: true
  })
  .then(() =&gt; {
  
      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
                  });
  
              });
      });
  });
  </code></pre>
  
  <p>It does fail with:</p>
  
  <blockquote>
    <p>Invalid value { name: 'Author 3' }</p>
  </blockquote>
  
  <p><strong>Edit 2 (table creation works)</strong></p>
  
  <p>Thanks to the suggestions from Ellebkey and some playing around, my model looks like this:</p>
  
  <pre><code>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'});
  </code></pre>
  
  <p>But using the create() code from the answer, I still get this:</p>
  
  <blockquote>
    <p>Unhandled rejection Error: Invalid value { name: undefined }</p>
  </blockquote>
  
  <p>Btw I am using sequelize 4.37.6</p>
  
  <p>Thanks in advance</p>