trisolaran

trisolaran

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 id.
I have a child table called child with ecto schema Child and a field args of type jsonb.
The field args is a json object with the following structrure:

{
  parent_id, <- this points to the primary key of "parent"
  ...
  <other_fields>
}

I would like to define a has_many association on the parent’s schema, something along these lines:

schema "parent" do 

  has_many :children, Child, foreign_key: fragment("(args->>'parent_id')::int")
end

I’m aware that this won’t work in Ecto, but it will hopefully convey what I’m trying to achieve.

I’m also aware that I could change the child’s table’s schema to add an ordinary foreign key, but it’s something I would like to avoid because the table and its schema are managed by an external library. Also, I don’t want to clutter the table with ad-hoc keys for this specific use case, as the same table will be used in other scenarios.

My workaround would have been to manage the children field myself without Ecto. That would of course work.

However, before I go down that path, I wanted to ask if there is a way to achieve this directly with Ecto. I suspect that this is not possible, but perhaps someone here has some suggestions on how to “trick” Ecto into recognizing this kind of association.

Thank you very much in advance!

Showing Posts 1 to 2

kokolegorille

kokolegorille

Hello and welcome,

My first thought would be to use a parent_id field to Child, but not inside jsonb.

But maybe giving some shape to this jsonb with the help of embeded schema will allow the use of normal associations.

trisolaran

trisolaran OP

Thanks for your answer and yeah, excited to be here :slight_smile:

As I mentioned, I know that I could add perent_id to Child, but I don’t want to do it because I don’t manage the child table or its schema. The child table is a general purpose table managed by a different library.

I can’t change the Child schema either, for the very same reason.

That said, if I could change the Child schema, your suggestion of “giving shape” to the jsonb field via an embedded schema would be an interesting one.
However, I doubt that it would work, because the foreign_key option in the has_many definition in the parent table requires a top-level field in the child schema. How would I pass a field in an embedded schema to it?

I thought about this a bit more yesterday and I believe that a decent way to solve this in Ecto is to use a has_many through association (the Child table does have primary keys). So I could create a parent_child join table with following schema:

defmodule ParentChild do

  schema "parent_child" do 
     belongs_to :parent, Parent
     belongs_to :child, Child
  end
end

and then:

defmodule Parent do
  schema "parent" do 
    has_many :parent_child, ParentChild

    has_many :children, Child, through: [:parent_child, :child] 
  end
end

That would solve the problem I believe, albeit at the cost of one additional table. I will take this idea for a spin soon.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews