Sequelize associate a array field to a model
See original GitHub issueI 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:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top 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 >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 >
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 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.
is there anyone who succeeded to solve the issue?