回购如何找出表格名称?

ank*_*981 0 elixir ecto phoenix-framework

我还有一个新手问题。当我做类似的事情

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

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

Dog*_*ert 6

Ecto __schema__在模块上定义一个函数use Ecto.Schema,然后调用schema do ... end。如果传递: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"
Run Code Online (Sandbox Code Playgroud)

各种参数__schema__接受被记录在这里