# \#associations

**URL:** https://forum.elixirforum.com/tag/associations/580.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [Is there a built-in Ecto way to define associations by an array of integers field?](https://forum.elixirforum.com/t/is-there-a-built-in-ecto-way-to-define-associations-by-an-array-of-integers-field/68124)

<div class="topic-metadata">

**Author:** [@acrolink](https://forum.elixirforum.com/u/acrolink)\
**Replies:** 7\
**Last updated:** [December 19, 2024, 2:18pm UTC](https://forum.elixirforum.com/t/is-there-a-built-in-ecto-way-to-define-associations-by-an-array-of-integers-field/68124 "2024-12-19T14:18:36Z")

</div>

Hi, Table A: Dishes: id and name fields. Table B: Users: with a field called favorite\_dishes holding an array of id’s (of dishes). Is there an Ecto way to treat this as an association ? e.g. for pre-loading ? How t…

---

## [Ecto schema create assoc without adding to struct](https://forum.elixirforum.com/t/ecto-schema-create-assoc-without-adding-to-struct/67242)

<div class="topic-metadata">

**Author:** [@brettbeatty](https://forum.elixirforum.com/u/brettbeatty)\
**Replies:** 3\
**Last updated:** [November 8, 2024, 5:35am UTC](https://forum.elixirforum.com/t/ecto-schema-create-assoc-without-adding-to-struct/67242 "2024-11-08T05:35:21Z")

</div>

Central to my app is the Account. I have a lot of schemas that belongs\_to Account. I like including corresponding has\_one/has\_many on Account for query/build\_assoc conveniences, but I don’t want to clutter the Account st…

---

## [Trouble updating an association - changes not persisted](https://forum.elixirforum.com/t/trouble-updating-an-association-changes-not-persisted/64339)

<div class="topic-metadata">

**Author:** [@patrickdavey](https://forum.elixirforum.com/u/patrickdavey)\
**Replies:** 6\
**Last updated:** [June 20, 2024, 11:22pm UTC](https://forum.elixirforum.com/t/trouble-updating-an-association-changes-not-persisted/64339 "2024-06-20T23:22:28Z")

</div>

I’m learning Ecto, and having issues trying to update following the docs. In my repo User has\_many UserOrganisations, and there’s boolean called home on UserOrganisations. I’m attempting to set them all to false. Firs…

---

## [How to perform bulk associations with ecto?](https://forum.elixirforum.com/t/how-to-perform-bulk-associations-with-ecto/60462)

<div class="topic-metadata">

**Author:** [@sharkmyster](https://forum.elixirforum.com/u/sharkmyster)\
**Replies:** 2\
**Last updated:** [December 23, 2023, 11:38am UTC](https://forum.elixirforum.com/t/how-to-perform-bulk-associations-with-ecto/60462 "2023-12-23T11:38:12Z")

</div>

I’m working on a game app that has an answers table and a categories table. This relationship is many to many. The admins for the app can add answers and categories individually using usual controller format. However, I…

---

## [Ecto get records where association count is 0](https://forum.elixirforum.com/t/ecto-get-records-where-association-count-is-0/34900)

<div class="topic-metadata">

**Author:** [@Owens](https://forum.elixirforum.com/u/Owens)\
**Replies:** 7\
**Last updated:** [November 30, 2023, 2:34pm UTC](https://forum.elixirforum.com/t/ecto-get-records-where-association-count-is-0/34900 "2023-11-30T14:34:20Z")

</div>

I couldn’t find an answer to this from searching so thought I would make a post about it. I’m trying to get a list of Users who have not created Office Hours. Essentially, records where the association does not exist or…

---

## [Optimistic lock solution for "embedded"/associated object. Entity mapped to Union](https://forum.elixirforum.com/t/optimistic-lock-solution-for-embedded-associated-object-entity-mapped-to-union/59610)

<div class="topic-metadata">

**Author:** [@1stSolo](https://forum.elixirforum.com/u/1stSolo)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 11:15pm UTC](https://forum.elixirforum.com/t/optimistic-lock-solution-for-embedded-associated-object-entity-mapped-to-union/59610 "2023-11-09T23:15:42Z")

</div>

Hello. I have a Parent Absinthe & Ecto schemas and there’s an embedded child object associated to it (1:1, so the fully inflated object is basically a union of the underlying tables, but it’s not implemented literally t…

---

## [When adding two foreign keys using has\_many only one is getting added](https://forum.elixirforum.com/t/when-adding-two-foreign-keys-using-has-many-only-one-is-getting-added/56213)

<div class="topic-metadata">

**Author:** [@chrisdel101](https://forum.elixirforum.com/u/chrisdel101)\
**Replies:** 7\
**Last updated:** [June 1, 2023, 4:45pm UTC](https://forum.elixirforum.com/t/when-adding-two-foreign-keys-using-has-many-only-one-is-getting-added/56213 "2023-06-01T16:45:38Z")

</div>

I have a Role like this in DB. It is attached to both a organization and a employee. But I cannot get both fields organization\_id && employee\_id to populate. This what it looks like after inserting an employee and an or…

---

## [How do I insert items with associations into my database via iex?](https://forum.elixirforum.com/t/how-do-i-insert-items-with-associations-into-my-database-via-iex/55655)

<div class="topic-metadata">

**Author:** [@aNerdInTheHand](https://forum.elixirforum.com/u/aNerdInTheHand)\
**Replies:** 2\
**Last updated:** [May 3, 2023, 2:44pm UTC](https://forum.elixirforum.com/t/how-do-i-insert-items-with-associations-into-my-database-via-iex/55655 "2023-05-03T14:44:06Z")

</div>

N.B. I don’t seem to have permission to post in the Phoenix section :woman\_shrugging: Background I’m learning Phoenix and my relational database skills are a bit rusty to say the least. I’m trying to create a data struc…

---

## [Insert with associated data, selecting associated data in changeset](https://forum.elixirforum.com/t/insert-with-associated-data-selecting-associated-data-in-changeset/55500)

<div class="topic-metadata">

**Author:** [@tino415](https://forum.elixirforum.com/u/tino415)\
**Replies:** 3\
**Last updated:** [April 26, 2023, 9:37am UTC](https://forum.elixirforum.com/t/insert-with-associated-data-selecting-associated-data-in-changeset/55500 "2023-04-26T09:37:45Z")

</div>

Hello all, I’m trying to implement oauth server and I’m not sure how to implement inserting token into database. For simplicity I have this schema: token client\_id ... scope name (string) ... token\_scope to…

---

## [How to associate an existing record using belongs\_to with Ecto?](https://forum.elixirforum.com/t/how-to-associate-an-existing-record-using-belongs-to-with-ecto/35785)

<div class="topic-metadata">

**Author:** [@thiagomajesk](https://forum.elixirforum.com/u/thiagomajesk)\
**Replies:** 11\
**Last updated:** [March 30, 2023, 12:25am UTC](https://forum.elixirforum.com/t/how-to-associate-an-existing-record-using-belongs-to-with-ecto/35785 "2023-03-30T00:25:36Z")

</div>

Hello everyone! Something happened at work today and it kinda got me off-guard because I was expecting different behavior from Ecto. What happened is: I was trying to insert a record with a given association that alread…

---

## ["manual" assocs with Ecto](https://forum.elixirforum.com/t/manual-assocs-with-ecto/54026)

<div class="topic-metadata">

**Author:** [@Sebb](https://forum.elixirforum.com/u/Sebb)\
**Replies:** 2\
**Last updated:** [February 21, 2023, 3:49am UTC](https://forum.elixirforum.com/t/manual-assocs-with-ecto/54026 "2023-02-21T03:49:38Z")

</div>

So, now I have to use a database, its been a long time since I did that, but I know some SQL. First steps were easy with Ecto+sqlite (dont even have to install a DB), but I struggle with the assocs stuff. There is this…

---

## [How to validate Ecto Associations properly?](https://forum.elixirforum.com/t/how-to-validate-ecto-associations-properly/51009)

<div class="topic-metadata">

**Author:** [@leon\_cruz](https://forum.elixirforum.com/u/leon_cruz)\
**Replies:** 8\
**Last updated:** [October 14, 2022, 5:20pm UTC](https://forum.elixirforum.com/t/how-to-validate-ecto-associations-properly/51009 "2022-10-14T17:20:22Z")

</div>

Hello there, I’m getting some problems in validate associations in Ecto. I have two schemas, Video and Playlist (a playlist can have many videos). The Video schema has a following changeset: def changeset(video, attrs \\…

---

## [Put\_assoc vs. put\_change in has\_many association](https://forum.elixirforum.com/t/put-assoc-vs-put-change-in-has-many-association/47697)

<div class="topic-metadata">

**Author:** [@silverdr](https://forum.elixirforum.com/u/silverdr)\
**Replies:** 2\
**Last updated:** [May 11, 2022, 11:43am UTC](https://forum.elixirforum.com/t/put-assoc-vs-put-change-in-has-many-association/47697 "2022-05-11T11:43:20Z")

</div>

So far I’ve been using put\_assoc/4 when adding associated “structs” to a changeset on record creation. Something like Ecto.Changeset.put\_assoc(:comments, \[%Comment{body: "example"}\]) but stumbled recently across a piec…

---

## [Updating structs with Ecto changesets and put\_assoc](https://forum.elixirforum.com/t/updating-structs-with-ecto-changesets-and-put-assoc/47488)

<div class="topic-metadata">

**Author:** [@hawkyre](https://forum.elixirforum.com/u/hawkyre)\
**Replies:** 7\
**Last updated:** [April 30, 2022, 11:55am UTC](https://forum.elixirforum.com/t/updating-structs-with-ecto-changesets-and-put-assoc/47488 "2022-04-30T11:55:12Z")

</div>

Hey there, this is my first post in here! I’m “just” in need of a little help with changesets and associations. I have a changeset I built that works wonders to create and validate structs. However, now that I want to a…

---

## [Best practices for defining Ecto associations](https://forum.elixirforum.com/t/best-practices-for-defining-ecto-associations/46339)

<div class="topic-metadata">

**Author:** [@jonbirke0927](https://forum.elixirforum.com/u/jonbirke0927)\
**Replies:** 4\
**Last updated:** [March 2, 2022, 11:35am UTC](https://forum.elixirforum.com/t/best-practices-for-defining-ecto-associations/46339 "2022-03-02T11:35:46Z")

</div>

Hello everyone, My team and I are looking for perspectives on best-practices when defining associations in Ecto. Currently, we are using a Postgres database, and have defined a number of schemas in Ecto, some of which h…

---

## [Adding a nested association in a changeset when I can't be sure of existing data/changes?](https://forum.elixirforum.com/t/adding-a-nested-association-in-a-changeset-when-i-cant-be-sure-of-existing-data-changes/45824)

<div class="topic-metadata">

**Author:** [@tomalexhughes](https://forum.elixirforum.com/u/tomalexhughes)\
**Replies:** 5\
**Last updated:** [February 6, 2022, 7:00pm UTC](https://forum.elixirforum.com/t/adding-a-nested-association-in-a-changeset-when-i-cant-be-sure-of-existing-data-changes/45824 "2022-02-06T19:00:07Z")

</div>

I’m creating a Phoenix LiveView application where a user can create an interview and add questions to that interview. I have a one-to-many relationship so that one interview can have many questions. There will always be …

---

## [Named associations?](https://forum.elixirforum.com/t/named-associations/44792)

<div class="topic-metadata">

**Author:** [@josefrichter](https://forum.elixirforum.com/u/josefrichter)\
**Replies:** 4\
**Last updated:** [December 23, 2021, 5:43pm UTC](https://forum.elixirforum.com/t/named-associations/44792 "2021-12-23T17:43:21Z")

</div>

Hi, is there a way to have named association, please? Akin to Rails/ActiveRecord as: option. Something like this: many\_to\_many :users, as: :members I just can’t find it anywhere. Which leads me to believe it’s probably…

---

## [Weird error updating many\_to\_many association \`no function clause matching in anonymous fn/2 in Ecto.Changeset.get\_changed/6\`](https://forum.elixirforum.com/t/weird-error-updating-many-to-many-association-no-function-clause-matching-in-anonymous-fn-2-in-ecto-changeset-get-changed-6/43935)

<div class="topic-metadata">

**Author:** [@wking-io](https://forum.elixirforum.com/u/wking-io)\
**Replies:** 6\
**Last updated:** [November 17, 2021, 5:48pm UTC](https://forum.elixirforum.com/t/weird-error-updating-many-to-many-association-no-function-clause-matching-in-anonymous-fn-2-in-ecto-changeset-get-changed-6/43935 "2021-11-17T17:48:31Z")

</div>

I cannot figure out what could be wrong here I copied the code directly from the many\_to\_many docs. def highlight\_verses(verse\_ids, member) do verses = get\_verses(verse\_ids) loaded = member |\> Repo.…

---

## [Dealing with associations with Ecto](https://forum.elixirforum.com/t/dealing-with-associations-with-ecto/41481)

<div class="topic-metadata">

**Author:** [@umbra](https://forum.elixirforum.com/u/umbra)\
**Replies:** 7\
**Last updated:** [August 6, 2021, 4:56pm UTC](https://forum.elixirforum.com/t/dealing-with-associations-with-ecto/41481 "2021-08-06T16:56:52Z")

</div>

So once again Im stuck, I think that I have some problem with “database frameworks”, Im struggling really bad with Ecto, mainly with associations. Maybe the main issue here is my database design, who may be bad, even if …

---

## [Foreign key in jsonb field for ecto association](https://forum.elixirforum.com/t/foreign-key-in-jsonb-field-for-ecto-association/41397)

<div class="topic-metadata">

**Author:** [@trisolaran](https://forum.elixirforum.com/u/trisolaran)\
**Replies:** 2\
**Last updated:** [August 2, 2021, 7:08am UTC](https://forum.elixirforum.com/t/foreign-key-in-jsonb-field-for-ecto-association/41397 "2021-08-02T07:08:00Z")

</div>

Hello :waving\_hand:, I’m trying to define a one-to-many association where the child’s foreign key is contained inside a jsonb field. I have a parent table (let’s just call it parent) with an integer primary key field i…

---

## [Polymorphic associations ecto](https://forum.elixirforum.com/t/polymorphic-associations-ecto/38780)

<div class="topic-metadata">

**Author:** [@romaluca](https://forum.elixirforum.com/u/romaluca)\
**Replies:** 1\
**Last updated:** [April 7, 2021, 11:59am UTC](https://forum.elixirforum.com/t/polymorphic-associations-ecto/38780 "2021-04-07T11:59:39Z")

</div>

Hi i have a table “activities” in my db with colums: id, user\_id, user\_to\_id, photo\_id, type type can be: ‘vote’, ‘user\_follow’, ‘photo\_follow’ I created a schema Vote, UserFollow, PhotoFollow. The schema of Vote is l…

---

## [Dynamic belongs\_to schema type resolution](https://forum.elixirforum.com/t/dynamic-belongs-to-schema-type-resolution/37693)

<div class="topic-metadata">

**Author:** [@overcomeoj](https://forum.elixirforum.com/u/overcomeoj)\
**Replies:** 8\
**Last updated:** [February 26, 2021, 2:07pm UTC](https://forum.elixirforum.com/t/dynamic-belongs-to-schema-type-resolution/37693 "2021-02-26T14:07:08Z")

</div>

Is there any way to describe the following relationship, such that you could write sale.item.price, where item might be a red OR green item? I can’t figure a great way to model it in ecto, but I also don’t think it’s…

---

## [Ecto has\_many through with condition/filter](https://forum.elixirforum.com/t/ecto-has-many-through-with-condition-filter/37485)

<div class="topic-metadata">

**Author:** [@sudochop](https://forum.elixirforum.com/u/sudochop)\
**Replies:** 5\
**Last updated:** [February 12, 2021, 4:51pm UTC](https://forum.elixirforum.com/t/ecto-has-many-through-with-condition-filter/37485 "2021-02-12T16:51:26Z")

</div>

I’ve got an existing database with an interesting permissions model. The permissions schema are based on a php library called Spatie Permissions. Long story short, I need to join my User’s roles through a model\_has\_roles …

---

## [Ecto: Unit Test for Validating Association Declarations](https://forum.elixirforum.com/t/ecto-unit-test-for-validating-association-declarations/37003)

<div class="topic-metadata">

**Author:** [@halostatue](https://forum.elixirforum.com/u/halostatue)\
**Replies:** 0\
**Last updated:** [January 21, 2021, 6:33pm UTC](https://forum.elixirforum.com/t/ecto-unit-test-for-validating-association-declarations/37003 "2021-01-21T18:33:04Z")

</div>

I’m sharing this because it may be of general interest. I was doing some refactoring of code and removed some automatic aliases when doing use MyApp.Schema, which broke some of my unit tests…but not all of them (I don’t …

---

## [Ecto Constraints when the primary key is a field not auto-generated](https://forum.elixirforum.com/t/ecto-constraints-when-the-primary-key-is-a-field-not-auto-generated/36812)

<div class="topic-metadata">

**Author:** [@nikolis](https://forum.elixirforum.com/u/nikolis)\
**Replies:** 4\
**Last updated:** [January 13, 2021, 4:49pm UTC](https://forum.elixirforum.com/t/ecto-constraints-when-the-primary-key-is-a-field-not-auto-generated/36812 "2021-01-13T16:49:05Z")

</div>

Hello Felas, I come accross the following problem and I need thoughts. I have a simple ecto schema with a signle string field seted as the primary key Parent. This has a has\_many relation with a normal ecto schema Chi…

---

## [Updating an associated schema of a belongs\_to association](https://forum.elixirforum.com/t/updating-an-associated-schema-of-a-belongs-to-association/36620)

<div class="topic-metadata">

**Author:** [@juddey](https://forum.elixirforum.com/u/juddey)\
**Replies:** 2\
**Last updated:** [January 12, 2021, 1:26am UTC](https://forum.elixirforum.com/t/updating-an-associated-schema-of-a-belongs-to-association/36620 "2021-01-12T01:26:59Z")

</div>

Got an (lv) app with the following associations: App.People.Person belongs\_to: family, App.People.Family App.People.Family has\_many(:members, App.People.Person) I’ve got the Person loaded, along with the associations …

---

## [Ecto Associations with custom fields](https://forum.elixirforum.com/t/ecto-associations-with-custom-fields/36458)

<div class="topic-metadata">

**Author:** [@beechnut](https://forum.elixirforum.com/u/beechnut)\
**Replies:** 3\
**Last updated:** [January 2, 2021, 2:12pm UTC](https://forum.elixirforum.com/t/ecto-associations-with-custom-fields/36458 "2021-01-02T14:12:14Z")

</div>

Hello! I am currently learning Elixir and Phoenix with the book “Phoenix in Action”. However, I am trying to implement the examples on an app that is usable for me. At the moment I’m stuck with the Ecto Associations. Th…

---

## [Ecto Preload with Condition from the Main Query](https://forum.elixirforum.com/t/ecto-preload-with-condition-from-the-main-query/35820)

<div class="topic-metadata">

**Author:** [@bagasprstyadi](https://forum.elixirforum.com/u/bagasprstyadi)\
**Replies:** 2\
**Last updated:** [November 27, 2020, 9:28am UTC](https://forum.elixirforum.com/t/ecto-preload-with-condition-from-the-main-query/35820 "2020-11-27T09:28:27Z")

</div>

Hi guys just wondering is it possible to preload but with a condition from the main query Let’s say I have a model named Post. A Post has a tag which is a string. Then I want to query a specific Post for example Post wi…

---

## [Why is put\_assoc calling Repo.update on the association?](https://forum.elixirforum.com/t/why-is-put-assoc-calling-repo-update-on-the-association/35663)

<div class="topic-metadata">

**Author:** [@augnustin](https://forum.elixirforum.com/u/augnustin)\
**Replies:** 1\
**Last updated:** [November 19, 2020, 3:11pm UTC](https://forum.elixirforum.com/t/why-is-put-assoc-calling-repo-update-on-the-association/35663 "2020-11-19T15:11:35Z")

</div>

When I write: belongs\_to(:associate, Associate, foreign\_key: :associate\_id, on\_replace: :nilify) def do\_associate(object, associated) do object |\> put\_assoc(:associate, associated) |\> Repo.update() end I have …

---

## [Preload only first nested association's association](https://forum.elixirforum.com/t/preload-only-first-nested-associations-association/35577)

<div class="topic-metadata">

**Author:** [@Owens](https://forum.elixirforum.com/u/Owens)\
**Replies:** 8\
**Last updated:** [November 16, 2020, 12:00pm UTC](https://forum.elixirforum.com/t/preload-only-first-nested-associations-association/35577 "2020-11-16T12:00:05Z")

</div>

Hello all, I’m doing a DB call for the index page of a WhatsApp-style chat app. I have ChatsUsers → Chats → Messages. I need to get the list of the user’s chats ordered by recency of the last\_message, and just for the…

[Next page](https://forum.elixirforum.com/tag/associations/580.md?match_all_tags=true&page=1&tags%5B%5D=associations)
