1
votes

I have an app that uses sequelize and we've gotten to the point where we have a lot of migrations. We decided that when we create new databases we want to skip the migrations and just build the database schema using the sequelize.sync command from the latest model definitions. I've been testing this approach and the command works as expected:

async function syncDb() {
  const res = await db.sequelize.sync({ force: true });
  process.exit();
}

but the problem is when I try to run seeders I get an error about missing columns that are not defined in the model and they're also not in the database when I inspect it using mysql workbench. For example, we have a table called 'practices', this table has a number of columns but no createdAt column. The generated table in mysql workbench doesnt have a createdAt column and the timestamps option is set to false.

In my seeder file I do a bulkInsert:

return queryInterface.bulkInsert('practices', [
  { data },
  etc...
]);

naturally, the data fields in the bulkInsert dont have any createdAt fields, but I keep getting this annoying error when I run sequelize db:seed:all --debug

== 20180515000000-practices: migrating =======

ERROR: SequelizeDatabaseError: Field 'createdAt' doesn't have a default value at Query.formatError (/Users/abdulahmad/Desktop/icanotes/development/api/node_modules/sequelize/lib/dialects/mysql/query.js:247:16) at Query.handler [as onResult] (/Users/abdulahmad/Desktop/icanotes/development/api/node_modules/sequelize/lib/dialects/mysql/query.js:68:23) at Query.execute (/Users/abdulahmad/Desktop/icanotes/development/api/node_modules/mysql2/lib/commands/command.js:30:14) at Connection.handlePacket (/Users/abdulahmad/Desktop/icanotes/development/api/node_modules/mysql2/lib/connection.js:449:32) at PacketParser.Connection.packetParser.p [as onPacket] (/Users/abdulahmad/Desktop/icanotes/development/api/node_modules/mysql2/lib/connection.js:72:12) at PacketParser.executeStart (/Users/abdulahmad/Desktop/icanotes/development/api/node_modules/mysql2/lib/packet_parser.js:75:16) at Socket.Connection.stream.on.data (/Users/abdulahmad/Desktop/icanotes/development/api/node_modules/mysql2/lib/connection.js:79:25) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at addChunk (_stream_readable.js:263:12) at readableAddChunk (_stream_readable.js:250:11) at Socket.Readable.push (_stream_readable.js:208:10) at TCP.onread (net.js:597:20)

this error doesn't tell me which seeder is failing, from the output, it looks like the practices seeder is the only one that ran so far.

So i'm trying to figure out where the issue is, since there's no createdAt in the practices table, and I'm not inserting any createdAt fields, I tried commenting out the entire seeder file's contents and I still get the error... how is that possible? the seeder isnt actually inserting anything, it looks like this now:

up: (queryInterface, Sequelize) => {
  // empty
}

is there a way to figure out what sql is running or a better way to debug what's causing this error?

1

1 Answers

-1
votes

As in documentation bulkInsert, We can pass additional options that are similar to options of bulkCreate. In bulkCreate options we can specify the fields to insert (defaults to all fields).

So you can use like this,

queryInterface.bulkInsert('practices', [{ data }], { fields: 'Array of all fields except createdAt' });