foreign key constraint error
See original GitHub issueI’m not super great with MySQL yet so im struggling with why this is happening. I had everything working fine and then I dropped the DB to start fresh and now the code no longer works. Here’s the error"
2015-09-12 23:03:16 12d144000 Error in foreign key constraint of table app/comments:
FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB:
You have defined a SET NULL condition though some of the
columns are defined as NOT NULL.
Here’s the SQL it’s trying to inject:
CREATE TABLE IF NOT EXISTS `posts` (`id` CHAR(36) BINARY , `title` VARCHAR(255) NOT NULL, `body` TEXT NOT NULL, `slug` TEXT, `cover_photo` TEXT, `created_at` DATETIME, PRIMARY KEY (`id`)) ENGINE=InnoDB;
SHOW INDEX FROM `posts`
CREATE TABLE IF NOT EXISTS `comments` (`id` CHAR(36) BINARY , `post_id` CHAR(36) BINARY NOT NULL, `body` TEXT NOT NULL, `created_at` DATETIME, PRIMARY KEY (`id`), FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Here’s my model definitions
var Sequelize = require('sequelize');
var orm = require('../utils/orm');
var Comments = require('./comment');
var Post = orm.define('post', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4, // Generates a UUID V4
primaryKey: true
},
title: {
type: Sequelize.STRING, // Should cap titles to 255 characters
allowNull: false
},
body: {
type: Sequelize.TEXT,
allowNull: false
},
slug: {
type: Sequelize.TEXT, // URLs can be any length
allowNull: true
},
cover_photo: {
type: Sequelize.TEXT, // URLs can be any length
allowNull: true
},
created_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
}
}, {
timestamps: false,
underscored: true
});
Post.hasMany(Comments);
Comments.belongsTo(Post);
module.exports = Post;
var Sequelize = require('sequelize');
var orm = require('../utils/orm');
var Comment = orm.define('comment', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4, // Generates a UUID V4
primaryKey: true
},
post_id: {
type: Sequelize.UUID,
allowNull: false
},
body: {
type: Sequelize.TEXT,
allowNull: false
},
created_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
}
}, {
timestamps: false,
underscored: true
});
module.exports = Comment;
I read in another post to try flipping the associations around, but it didn’t help. I tried flipping it to:
Comments.belongsTo(Post);
Post.hasMany(Comments);
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Top Results From Across the Web
3 common foreign key mistakes (and how to avoid them)
1. Mismatched data types ... When you're using a foreign key to reference a column in another table, the datatypes of both tables...
Read more >Why do I get a foreign key constraint error - sql - Stack Overflow
If you are trying to INSERT a value into the table Articulos that does not exist in the table Fabricantes then you will...
Read more >FIX: A conflict with the foreign key constraint occurs when you ...
This problem occurs because values that are equal at the type level may be distinct at the binary level. For example, at the...
Read more >MySQL Error Code 1215: "Cannot add foreign key constraint"
In this blog, we'll look at how to resolve MySQL error code 1215: “Cannot add foreign key constraint”. Our Support customers often come...
Read more >Primary Key and Foreign Key Errors to Avoid - DATAVERSITY
Primary Key and Foreign Key Errors to Avoid · Dangling foreign keys. A foreign key points to a primary key that isn't there....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I had something similar happen to me. I simply removed the foreign keys from the model definition and let the associations create it for me. When you attach associations Sequelize will create those foreign keys for you. I think the error is that they already exist because you defined them in the model. Try removing the
post_idcolumn fromCommentsmodel. Please let us know if it works.I too faced these same issues, Thanks for the Solution