我为没有字段的表生成并运行了迁移id(使用该字段league_id作为主键)。我确认迁移按预期进行(检查了 psql 中的数据库,该表确实没有id字段并且league_id是主键)。
当我运行时,Repo.all(League)出现以下错误:
13:01:14.962 [debug] QUERY ERROR source="leagues" db=2.8ms
SELECT l0."id", l0."league_id" FROM "leagues" AS l0 []
** (Postgrex.Error) ERROR 42703 (undefined_column): column l0.id does not exist
有没有办法告诉Repo.all/1没有 id 字段(除了手动构建SELECT *-type 查询之外?)
如果您使用与 不同的主键列id,则可以@primary_key在声明架构时使用该属性指定:
# See documentation link below for what values the options list accepts.
@primary_key {:league_id, :id, []}
schema "leagues" do
...
end
Run Code Online (Sandbox Code Playgroud)
或者,如果您不想在查询中使用该列,则可以将键设置为false:
@primary_key false
schema "leagues" do
...
end
Run Code Online (Sandbox Code Playgroud)
该文档详细解释了这一点。