生成引用自列名称错误的模型

时间:2016-07-13 11:19:16

标签: phoenix-framework ecto

给定(从头开始)

mix phoenix.new sandpit
mix ecto.create
mix phoenix.gen.json Part parts name part_of:references:parts
mix ecto.migrate

并编辑路线

scope "/api", Sandpit do
    pipe_through :api
    resources "/parts", PartController, except: [:new, :edit]
  end

然后运行服务器并转到

http://localhost:4000/api/parts

我收到了错误

info] Running Sandpit.Endpoint with Cowboy using http on port 4000
13 Jul 23:11:37 - info: compiled 5 files into 2 files, copied 3 in 744ms
[info] GET /api/parts
[debug] Processing by Sandpit.PartController.index/2
  Parameters: %{}
  Pipelines: [:api]
[debug] SELECT p0."id", p0."name", p0."part_of_id", p0."inserted_at", p0."updated_at" FROM "parts" AS p0 [] ERROR query=78.0ms
[info] Sent 500 in 140ms
[error] #PID<0.318.0> running Sandpit.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/parts
** (exit) an exception was raised:
    ** (Postgrex.Error) ERROR (undefined_column): column p0.part_of_id does not exist
        (ecto) lib/ecto/adapters/sql.ex:185: Ecto.Adapters.SQL.query!/5

这是发电机的错误吗?它似乎想要用_id来命名,或者它看起来像一个隐藏的“规则”,或者它无法在_id中添加。如果它没有自动添加_id,我认为要么生成错误,要么使用给定的名称而不会崩溃?

尝试

mix phoenix.gen.json Thing things name thing_of_id:references:things

并且迁移不会导致错误

或者还有其他事情发生了吗?

1 个答案:

答案 0 :(得分:0)

已知问题,目前正在讨论最佳解决方案: -

https://github.com/phoenixframework/phoenix/issues/1605

目前的解决方案是记住使用_id

后缀引用