matt12
To save on unnecessary indices, I would like to use the primary key as a foreign key (in my case because I am using class table inheritance).
So I have something like this as a migration:
create table(:base_class) do
...
end
create table(:derived_class) do
add :base_class_id, :integer, references(:base_class),
null: false,
primary_key: true
...
end
Then, on the schema side I would like to use something like this to facilitate reuse in all derived classes (idea from the Ecto migration guide):
defmodule MyApp.DerivedClassExtensionSchema do
defmacro __using__(_) do
quote do
use Ecto.Schema
@primary_key {:base_class_id, :id, autogenerate: false}
end
end
end
Finally, the derived class schema looks like this:
defmodule MyApp.DerivedClass do
use MyApp.DerivedClassExtensionSchema
schema "derived_class" do
belongs_to :base, MyApp.BaseClass
end
end
The problem is that when I try to do this, I get a (ArgumentError) field/association :base_class_id is already set on schema.
Am I adding the primary key twice or something? My thinking is that the migration is what actually modifies the database, the schema is only a mapping from the database to Elixir structs, and the schema must be told if you plan to use a different PK than the default. But I am getting this error and I am not sure how to proceed.
Thanks for reading.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
matt12
The problem with this appeared to be:
primary_key: falseargument with thecreate tablefunction. This took care of things on the DB side.belongs_tofunction needed an optionprimary_key: true. Unfortunately, this did not take care of things on the schema side of things, as using an empty struct modeled after the schema still included anidfield that is autogenerated–and a new problem was born. I tried to pass anautogenerated: falseoption but this is not supported in thebelongs_tofunction. So… Unless there is a way to stop autogenerating anidcolumn on the schema side of things, maybe this is a bug?