【问题标题】:Customize ecto migration primary key and autogenerate自定义ecto迁移主键并自动生成
【发布时间】:2018-01-22 14:09:43
【问题描述】:

我想创建一个用户表,其中包含自动生成的 aid 和我自己处理的 id。

我尝试了以下迁移和架构:

迁移:

def change do
  create table(:users, primary_key: false) do
    add :aid, :id, autogenerate: true
    add :id, :integer
    add :first_name, :string
    add :last_name, :string
    add :email, :string
    add :phone, :string
    add :language, :string
    add :password_hash, :string
    add :verified_email, :utc_datetime
    add :verified_phone, :utc_datetime

    timestamps()
  end

运行此迁移时发生的情况是 aid 列未添加到我的 user 表中

我怎样才能根据需要更改数据库?

【问题讨论】:

  • 我刚刚在本地尝试过,它确实包含了辅助列,只是它不是主键。我相信你应该使用primary_key: true 而不是autogenerate: true
  • 感谢@JoséValim 的帮助,我实现了所需的功能:add :aid, :bigserial, primary_key: true

标签: database postgresql elixir phoenix-framework ecto


【解决方案1】:

您应该使用primary_key: true 而不是autogenerate: true

add :aid, :id, primary_key: true

【讨论】:

    猜你喜欢
    • 2019-08-29
    • 2020-06-20
    • 2018-08-24
    • 1970-01-01
    • 2011-09-18
    • 2011-01-01
    • 2013-02-15
    • 1970-01-01
    相关资源
    最近更新 更多