Elixir 回购协议如何计算出该表的名称?

Elixir 回购协议如何计算出该表的名称?,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我还有个问题。当我做这样的事情时 case MyRepo.insert %Post{title: "Ecto is great"} do {:ok, struct} -> # Inserted with success {:error, changeset} -> # Something went wrong end Repo如何知道要使用数据库中的哪个表?Ecto在调用use-Ecto.schema的模块上定义了一个\u-schema\u函数,然后调用sche

我还有个问题。当我做这样的事情时

case MyRepo.insert %Post{title: "Ecto is great"} do
  {:ok, struct}       -> # Inserted with success
  {:error, changeset} -> # Something went wrong
end

Repo如何知道要使用数据库中的哪个表?

Ecto在调用
use-Ecto.schema
的模块上定义了一个
\u-schema\u
函数,然后调用
schema-do。。。结束
。如果将
:source
传递给它,则返回表名

iex(1)> %MyApp.Post{}
%MyApp.Post{__meta__: #Ecto.Schema.Metadata<:built, "posts">,
 comments: #Ecto.Association.NotLoaded<association :comments is not loaded>,
 id: nil, inserted_at: nil, title: nil, updated_at: nil}
iex(2)> %MyApp.Post{}.__struct__
MyApp.Post
iex(3)> %MyApp.Post{}.__struct__.__schema__(:source)
"posts"
iex(1)>%MyApp.Post{}
%MyApp.Post{{uuuuu meta}:{35; exto.Schema.Metadata,

注释:#exto.Association.notload.

太好了!这意味着表名最终来自模式名,对吗?是的,模式的第一个参数是“源”或表名。