Table cannot have association with itself?
See original GitHub issueIn the associations documentation, it appears that a table can have an association with itself.
http://sequelizejs.com/docs/latest/associations#block-0-line-27.
// Or let's define some self references
var Person = sequelize.define('Person', { /* ... */})
Person.hasOne(Person, {as: 'Father'})
// this will add the attribute FatherId to Person
// also possible:
Person.hasOne(Person, {as: 'Father', foreignKey: 'DadId'})
// this will add the attribute DadId to Person
// In both cases you will be able to do:
Person#setFather
Person#getFather
I cannot seem to get this to work in Express. My code:
module.exports = function(sequelize, DataTypes) {
var Person = sequelize.define('Person', {
name: DataTypes.STRING
}, {
classMethods: {
associate: function (models) {
Person.belongsTo(models.Family);
Person.hasOne(Person, {as:'Parent', foreignKey: 'DadId'});
}
}
});
return Person;
};
The Table gets created, but is missing the column for the parent (Dad) I expected to be there. Is this still supported, or am I doing something wrong? Thank you.
Issue Analytics
- State:
- Created 9 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Why would a table use its primary key as a foreign key to itself
I've seen that a table can have a foreign key to itself to build a hierarchy structure, but it would use another column...
Read more >SQL table: create 1-to-1 relationship with itself? - Stack Overflow
I am currently working with a link table, MenuItemParent , but I can't figure out how to get the keys and constraints correctly....
Read more >how to create self-referencing data in an table - MSDN
I am dealing with a data base, where an entity named staff has a self relation 1-m(name of relation mentor), if means a...
Read more >Is it possible to create a "self-relationship" in one table?
Found the answer here and tested. You just have to open the same table 2 times and create the relationship between them as...
Read more >Guidelines: Association
This does not necessarily mean that an instance of that class has an association to itself; more often, it means that one instance...
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
Disregard last, I figured out what you were asking for.
So clearly it was working. The database admin tool that I was using didn’t add the additional column until I exited the application and reopened it.
I apologize for the false positive on this, I’ll get a better db tool.
Alas, belongsTo and hasOne both have the same result.
Thank you for you help and sorry once again.
I’m not sure how to see the table creation log.
Right now I’m using
db.sequelize.sync({ force: true }).complete(function(err) {...});
and it doesn’t seen to dump to the console log. All the other calls do. Is there a way to enable this?