trisolaran

trisolaran

Foreign key in jsonb field for ecto association

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!

Marked As Solved Switch mode

trisolaran

trisolaran

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.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement