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.

One to Many relation on Foreign Key column - Postgres 9.5.3

See original GitHub issue

I’m having issues setting up the relations for a one to many relationship where the “many" table references the “one” table through a non-primary key column on the “one” table. The schema has already been predetermined, so I’m not able to edit it at all.

Postgres v9.5

Here’s the models:

var Offer = sequelize.define(‘offer’,{ id: { type: Sequelize.INTEGER, primaryKey: true, allowNull: false, field: "id" }, otherid:{ type: Sequelize.STRING, allowNull: false, field: “otherid" }); var Attachment = sequelize.define(‘attachment’,{ id: { type: Sequelize.INTEGER, primaryKey: true, allowNull: false, field: "id" }, otherid:{ type: Sequelize.STRING, allowNull: false, field: “otherid" }, parentid:{ type: Sequelize.STRING, allowNull: true, field: "parentid", references: { model: Offer, key: "otherid" } } )

Each Offer should have many Attachments.

I set up the relations like this:

Attachment.belongsTo(Offer, {foreignKey:'parentid', targetKey:’otherid'}); Offer.hasMany(Attachment, {as: 'Attachments',foreignKey:'otherid'});

I know the Attachment.belongsTo relation is set up correctly, since I’m able to query for it and get the related Offer object returned:

attachments.findOne({ include: [ {model: m_offerCreatives} ] })

However, when I go to find the Offers, I get an error saying they aren’t related:

Offer.findAll({ include: [ {model: Attachment, as:'Attachments'} ]})

Error: “attachment (Attachments) is not associated to offer!”

I’ve tried setting this up every way I can think of from the docs, including defining Offer.belongsToMany(Attachment), but since there is no join table, I can’t seem to get it correct. I tried to do something similar for a Many-to-Many relationship with a join table that stores non-primary keys, but wasn’t able to get that to work either (I think it was the same issue in #4092).

Is it possible to have this one-to-many relationship on a non-primary key column?

Thanks!

Dialect: postgres Database version: 9.5.3 Sequelize version: 3.16

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
janmeiercommented, Aug 10, 2016

@felix yes, but it’s broken for belongs to many (have to get that fixed soon)…

@jessedavid you need target key on both sides of the association

0reactions
JesseDavidcommented, Aug 10, 2016

I added the targetKey but still got the same error and the SQL doesn’t look like it updated:

    Offer.hasMany(Attachment, {as: 'Attachments', foreignKey: 'other', targetKey: 'parentid'});

(SQL the same)

I’m using Typescript and the interface definition for hasMany doesn’t seem to have “targetKey” defined (not sure if this is a typings error or not).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 5.4. Constraints - PostgreSQL
A table can have more than one foreign key constraint. This is used to implement many-to-many relationships between tables. Say you have tables...
Read more >
Relationships - postgresql - Hasura
A one-to-one relationship between two tables can be established via a unique foreign key constraint. Say we have the following two tables in...
Read more >
Single foreign key for referencing one of multiple tables
My only suggestion would to add a extra_type field to explicitly identify whether the record is of type person or car .
Read more >
schema migration: one-to-many, many-to-many in PostgreSQL
Usually, one would simply connect them via Foreign Key relationships. Is there a reason why that won't work in your case? – khampson....
Read more >
Foreign Key Constraint in PostgreSQL - TutorialsTeacher
Thus, it forms one-to-many relationships between the employee and department table, which means for one department there could be multiple employees. In other ......
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