Sam Houston
Sam Houston

Reputation: 3651

Customize ecto migration primary key and autogenerate

I would like to create a users table with an aid which is autogenerated and an id which I handle myself.

I have attempted the following migration and schema:

Migration:

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

What is happening when I run this migration is that the aid column is not being added into my user table

How can I get this to change the database as I would like?

Upvotes: 1

Views: 1292

Answers (1)

José Valim
José Valim

Reputation: 51349

You should use primary_key: true instead of autogenerate: true:

add :aid, :id, primary_key: true

Upvotes: 2

Related Questions