Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Data Relationships with SQL and Sequelize Create Related Data Using Sequelize Models Test Your Changes

Seth Missiaen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seth Missiaen
Web Development Techdegree Graduate 21,652 Points

Sequelize validation error for directorPersonID

Sequelize is unable to read the id from the bradBird variable. I get this error:

Validation errors:  [ 'Movie.directorPersonID cannot be null' ]

I tried converting the array of people to json with this code:

const peopleJSON = peopleInstances.map(person => person.toJSON());
    [bradBird, vinDiesel, eliMarienthal, craigTNelson, hollyHunter] = peopleJSON;

when I console.log(bradBird.id) I am returned the correct number (1), but when trying to create each movie instance, I get the above validation error. Here is my code for the movie instances:

const movieInstances = await Promise.all([
      Movie.create({
        title: "The Iron Giant",
        releaseYear: 1999,
        directorPersonId: bradBird.id
      }),
      Movie.create({
        title: "The Incredibles",
        releaseYear: 2004,
        directorPersonId: bradBird.id
      }),
    ]);

UPDATE: Though I can't tell why this helped, I replaced my code in the models where I made the association by copying and pasting the code form the instruction into my text editor. I read through it multiple times and did not see any difference in the characters used. Here's the code that I had written:

Person.associate = (models) => {
    Person.hasMany(models.Movie, {
      foreignKey: {
        fieldName: 'directorPersonID',
        allowNull: false,
      },
    });
  };

And here's the code pasted in from the instruction page:

Person.associate = (models) => {
  Person.hasMany(models.Movie, {
    foreignKey: {
      fieldName: 'directorPersonId',
      allowNull: false,
    },
  });
};

The only thing that I can think of that could be impacting this is maybe the encoding? I started the project on my M1 mac mini, but then created a repo and downloaded that onto my intel macbook pro. I don't know if there's something different about how the files are written on the M1. Any insight would be greatly appreciated.

Also, the validation error above went away when I started pasting the code, and this error appeared in the terminal:

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error
    at Database.<anonymous> (/Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/node_modules/sequelize/lib/dialects/sqlite/query.js:179:27)
    at /Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/node_modules/sequelize/lib/dialects/sqlite/query.js:177:50
    at new Promise (<anonymous>)
    at Query.run (/Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/node_modules/sequelize/lib/dialects/sqlite/query.js:177:12)
    at /Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/node_modules/sequelize/lib/sequelize.js:314:28
    at async SQLiteQueryInterface.createTable (/Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/node_modules/sequelize/lib/dialects/abstract/query-interface.js:98:12)
    at async Function.sync (/Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/node_modules/sequelize/lib/model.js:942:7)
    at async Sequelize.sync (/Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/node_modules/sequelize/lib/sequelize.js:376:9)
    at async /Users/seth/Documents/Programming/Web Development/practice/sql/sequelize-practice-2/app.js:30:5 {
  name: 'SequelizeDatabaseError',
  parent: [Error: SQLITE_ERROR: duplicate column name: directorPersonID] {
    errno: 1,
    code: 'SQLITE_ERROR',
    sql: 'CREATE TABLE IF NOT EXISTS `Movies` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(255) NOT NULL, `releaseYear` INTEGER NOT NULL, `directorPersonId` INTEGER NOT NULL REFERENCES `People` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE, `directorPersonID` INTEGER NOT NULL REFERENCES `People` (`id`) ON DELETE CASCADE ON UPDATE CASCADE);'
  },
  original: [Error: SQLITE_ERROR: duplicate column name: directorPersonID] {
    errno: 1,
    code: 'SQLITE_ERROR',
    sql: 'CREATE TABLE IF NOT EXISTS `Movies` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(255) NOT NULL, `releaseYear` INTEGER NOT NULL, `directorPersonId` INTEGER NOT NULL REFERENCES `People` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE, `directorPersonID` INTEGER NOT NULL REFERENCES `People` (`id`) ON DELETE CASCADE ON UPDATE CASCADE);'
  },
  sql: 'CREATE TABLE IF NOT EXISTS `Movies` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` VARCHAR(255) NOT NULL, `releaseYear` INTEGER NOT NULL, `directorPersonId` INTEGER NOT NULL REFERENCES `People` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE, `directorPersonID` INTEGER NOT NULL REFERENCES `People` (`id`) ON DELETE CASCADE ON UPDATE CASCADE);',
  parameters: {}
}

Thanks for the help!