Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Seth M
seal-mask
.a{fill-rule:evenodd;}techdegree
Seth M
Web Development Techdegree Student 14,529 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!