如何指示 ecto 不创建自动递增 id 字段?

how to instruct ecto to not create the auto increment id field?

Ecto 迁移会在 table 中按名称 'id' 自动创建一个自动递增字段。

  1. 如何避免创建该字段?
  2. 如何将table中的另一列设置为主键(不是自增)?

您可以使用 primary_key: false 选项到 table/2 选择退出自动生成的主键列。您可以使用 primary_key: true 选项将另一列设置为主键 add/3:

create table(:users, primary_key: false) do
  add :my_id, :integer, primary_key: true
  add :name, :string
  # ...
end

更多信息,请参考文档:

http://hexdocs.pm/ecto/0.11.3/Ecto.Migration.html#table/2 http://hexdocs.pm/ecto/0.11.3/Ecto.Migration.html#add/3