question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Table cannot have association with itself?

See original GitHub issue

In 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:closed
  • Created 9 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
deepcover96commented, Jun 21, 2014

Disregard last, I figured out what you were asking for.

 -- Table: "Persons"

 -- DROP TABLE "Persons";

 CREATE TABLE "Persons"
 (
   id serial NOT NULL,
   name character varying(255),
   "createdAt" timestamp with time zone NOT NULL,
   "updatedAt" timestamp with time zone NOT NULL,
   "FamilyId" integer,
   "ParentId" integer,
   CONSTRAINT "Persons_pkey" PRIMARY KEY (id)
 )
 WITH (
   OIDS=FALSE
 );
 ALTER TABLE "Persons"
   OWNER TO postgres;

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.

 Person.hasOne(Person, {as: 'Parent'});

 Person.belongsTo(Person, {as: 'Parent'});

Thank you for you help and sorry once again.

0reactions
deepcover96commented, Jun 20, 2014

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found