#Is schema name the last part of module name?

26 messages · Page 1 of 1 (latest)

tawdry compass
#

I'm postfixing schema modules Entity. And I though actual schema name would be schema <this>.
But has_many looks for entity_id instead of <schema>_id.

Is schema name inferred from the module name? Or am I missing something?

defmodule Feder.Auth.Account.Entity do
  use Feder, :entity

  schema "account" do
    field :email, :string
    # invalid association `access` in schema Feder.Auth.Account.Entity: associated schema Feder.Auth.Access.Entity does not have field `entity_id`
    has_many :access, Feder.Auth.Access.Entity

    timestamps()
  end

  ...
end
summer bridge
#

yes, it is inferred. your schema is missing belongs_to

analog jungle
summer bridge
#

there is no belongs_to so there is no field no matter what you name the foreign key

analog jungle
#

but the docs say:

defaults to the underscored name of the current schema suffixed by _id

tawdry compass
#

I do have belongs_to on the other side and I can silence it with foreign_key

#

OK I'm just surprised by how libraries use the last part of the module name...

summer bridge
#

damn I thought it was referring to itself, I'm too tired 😄

tawdry compass
#

Isn't it more sensible to use the table name? Why the last part of module name?

analog jungle
#

yeah, there's several spots in ecto and Phoenix that use the module name...

tawdry compass
#

LiveView has similar affinity to the last part

summer bridge
#

schemas aren't connected to tables, you can have many schemas for one table

analog jungle
#

yeah and you can actually dynamically switch out the table for a schema at query time too

tawdry compass
#

Then all the has_many will infer the same name!

summer bridge
#

they won't because it's based on the module name, not table name

#

schemas might not even have tables at all

#

that's why it's based on the module name, not the table name

tawdry compass
#

Oh okay,

#

what should I look it up to understand the concept? I knuw basics of SQL

#

I thought schema > table all the time and it's what StackOverflow gives me

summer bridge
#

there is nothing requiring that. maybe you have one table where you have two schemas that read different fields from the table. or you can use Ecto to manage data that is not in a DB at all

tawdry compass
#

Oh okay, so schema is a way to look at a table?

summer bridge
#

might be. a schema is a schema and it might or might not be related to a table

tawdry compass
#

I see, docs says it's just source, not a table, thanks!

sacred juniper
#

Wasnt schema like prototype of the table btw??? Or the structure of the table

#

Is it different in elixir if a table has two schemas does that mean table has two different structures at a time how is that possible