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.

Sequelize associate a array field to a model

See original GitHub issue

I need to associate two tables one of them have a array field (the values is the id of that another one) and generating a query.

on table associations i have tried belongsToMany and just belongsTo. No success.

this is the query I hope to generate through sequelize

    basket.basket_id, as BasketId,
    item.item_id as ItemId,
    item.description as ItemDescription 
from  basket
inner join item on
    item.item_id = any(basket.item_array) 
where
    "some field"

the structure of tables

CREATE TABLE basket(
  basket_id SERIAL PRIMARY KEY,
  customer_id integer,
  item_array INTEGER[],
... some other fields
);

CREATE TABLE item(
  item_id SERIAL PRIMARY KEY,
  description text NOT NULL,
... some other fields
);

I think it would be very useful to make this kind of association in the sequelizes core

Im using sequelize with postgres. (latest version of both).

  • No. This issue is relevant to Sequelize as a whole.
  • [x ] Yes. This issue only applies to the following dialect(s): dialects with nosql support

Would you be willing to resolve this issue by submitting a Pull Request? No

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • [x ] No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ephyscommented, Jan 5, 2022

I don’t know if such a feature is going to be added to sequelize unless a dialect starts supporting arrays of foreign keys.

I would recommend using a through-table. Or, if you really want to use an array of FKs, https://github.com/sequelize/sequelize/issues/11788 is the issue about being able to define custom joins.

0reactions
ardaorkincommented, Jan 5, 2022

is there anyone who succeeded to solve the issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize define association on Array - Stack Overflow
1 Answer 1 · This doesn't work for me. · This will create class_id column on User which refers to id of Class...
Read more >
ARRAY | Sequelize
An easy-to-use multi SQL dialect ORM for Node.js.
Read more >
Associations - Sequelize
Sequelize supports the standard associations: One-To-One, One-To-Many ... Let's say we have two models, A and B . Telling Sequelize that you ...
Read more >
Advanced M:N Associations | Sequelize
Notice that the outer object is an User , which has a field called profiles , which is a Profile array, such that...
Read more >
Model Basics | Sequelize
Models are the essence of Sequelize. A model is an abstraction that represents a table in your database. In Sequelize, it is a...
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