【发布时间】:2019-11-03 20:07:19
【问题描述】:
当我尝试从数据库中获取列表时出现此错误,我不明白为什么。
这是引发错误的函数:
def list_lectures do
Lecture
|> Repo.all()
|> Repo.preload(:author [user: :credential])
end
这是我运行代码时遇到的错误:
iex(1)> Ram.CMS.list_lectures
[debug] QUERY OK source="lectures" db=3.4ms decode=0.7ms queue=0.4ms
SELECT l0."id", l0."source", l0."theme", l0."time", l0."title", l0."author_id", l0."inserted_at", l0."updated_at" FROM "lectures" AS l0 []
** (FunctionClauseError) no function clause matching in Access.get/3
The following arguments were given to Access.get/3:
# 1
:author
# 2
[user: :credential]
# 3
nil
Attempted function clauses (showing 5 out of 5):
def get(%module{} = container, key, default)
def get(map, key, default) when is_map(map)
def get(list, key, default) when is_list(list) and is_atom(key)
def get(list, key, _default) when is_list(list)
def get(nil, _key, default)
(elixir) lib/access.ex:265: Access.get/3
(ram) lib/ram/cms.ex:24: Ram.CMS.list_lectures/0
iex(1)>
schema "lectures" do
field :source, :string
field :theme, :string
field :time, :string
field :title, :string
belongs_to :author, Author
timestamps()
end
schema "credentials" do
field :email, :string
field :password, :string
belongs_to :user, User
timestamps()
end
schema "users" do
field :name, :string
field :role, :string
has_one :credential, Credential
timestamps()
end
【问题讨论】:
-
我认为预加载应该是
[:author, [user: :credential]]。您缺少分号。 -
谢谢,TheAnh,我按照你说的做了,但还是有问题。