I encountered an issue while trying to populate a database with seed data. The error message I received is:
name: 'SequelizeDatabaseError',
parent: Error: Column 'id' cannot be null
code: 'ER_BAD_NULL_ERROR',
errno: 1048,
sqlState: '23000',
sqlMessage: "Column 'id' cannot be null"
Below is the code snippet used to seed the data:
const seedUsers = require('./user-seeds');
const seedPosts = require('./post-seeds');
const seedComments = require('./comment-seeds');
const seedVotes = require('./vote-seeds');
const sequelize = require('../config/connection');
const seedAll = async () => {
await sequelize.sync({ force: true });
console.log('--------------');
await seedUsers();
console.log('--------------');
await seedPosts();
console.log('--------------');
await seedComments();
console.log('--------------');
await seedVotes();
console.log('--------------');
process.exit(0);
};
seedAll().catch(err => console.log('seedAll error: ', err));
The error specifically occurs during the execution of `seedPost()`. Here is the relevant model:
const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/connection');
// Post Model definition
class Post extends Model {
// Method for upvoting posts
static upvote(body, models) {
return models.Vote.create({
user_id: body.user_id,
post_id: body.post_id
}).then(() => {
// Additional logic for retrieving post and related data
});
}
}
// Define fields/columns for Post model
Post.init(
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoincrement: true
},
title: {
type: DataTypes.STRING,
allowNull: false
},
post_url: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isURL: true
}
},
user_id: {
type: DataTypes.INTEGER,
references: {
model: 'user',
key: 'id'
}
}
},
{
sequelize,
freezeTableName: true,
underscored: true,
modelName: 'post'
}
);
module.exports = Post;
This is how my seed file appears:
const { Post } = require('../models');
const postdata = [
{
title: 'Donec posuere metus vitae ipsum.',
post_url: 'https://buzzfeed.com/in/imperdiet/et/commodo/vulputate.png',
user_id: 10
},
{
// ... additional seed data
}
];
const seedPosts = () => Post.bulkCreate(postdata);
module.exports = seedPosts;
If you have any insights on what might be causing this issue, your assistance would be highly appreciated!