dtcaciuc

dtcaciuc

Both has_one and has_many pointing to the same schema

Hello everyone,

I’m looking for the correct way to declare a pair of schemas for a
document/revision system that would look something like this:

defmodule Document do
  use Ecto.Schema
  schema "documents" do
    field :created_by, :string
    has_many :revisions, DocumentRevision
    has_one :current_revision, DocumentRevision
  end
end

defmodule Document do
  use Ecto.Schema
  schema "documents_revisions" do
    field :revised_by, :string
    field :content, :string

    belongs_to :documents, Document
  end
end

where migration that defines the schema looks like the following

create table("documents") do
  add :created_by, :string
  add :current_revision_id, references(:document_revisions)
end

create table("document_revisions") do
  add :revised_by, :string
  add :content, :string
  add :document_id, references(:documents)
end

The immediate difficulty I’m having is updating the current_revision field.
I’m not sure what to pick for on_replace: option and updating current_revision_id
manually does not work either the following throws field latest_revision_id in update does not exist in schema Document in query when passed to update_all:

def set_revision(document, revision) do
  from(
    d in Document,
    where: d.id == ^document.id,
    update: [set: [current_revision_id: ^revision.id]]
  )

What would be a way to solve the above? The entities are not set in stone so if
there’s a better way to implement the general idea, I’m all ears as well.

Marked As Solved

al2o3cr

al2o3cr

This should be belongs_to :current_revision, DocumentRevision - the belongs_to always goes on the schema where the _id column is.

Also Liked

al2o3cr

al2o3cr

Yep, this kind of thing has been a headache since belongs_to and has_one (and surely for longer with different naming) :stuck_out_tongue:

https://github.com/rails/rails/blob/db045dbbf60b53dbe013ef25554fd013baf88134/activerecord/lib/active_record/associations.rb#L39-L51

This is from the initial commit to Rails in November 2004!

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement