Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

elixir

Ecto migrations automatically create an auto increment field by name 'id' in the table.

  1. How to avoid creating this field?
  2. How to set another column in the table as primary key (not auto increment)?
like image 280
shankardevy Avatar asked May 26 '15 18:05

shankardevy


People also ask

Is primary key auto increment by default?

No. A primary key must be unique and that has to be 100% guaranteed, and NON NULL A primary key should be stable if ever possible and not change.

How can create auto increment serial number in SQL Server?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .

Does Postgres auto increment primary key?

By simply setting our id column as SERIAL with PRIMARY KEY attached, Postgres will handle all the complicated behind-the-scenes work and automatically increment our id column with a unique, primary key value for every INSERT .

How do I set auto increment to 1?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name. The name of the table whose AUTO_INCREMENT value you wish to change.


1 Answers

You can opt out of the automatically generated primary key column with the primary_key: false option to table/2. You can set another column as primary key with the primary_key: true option to add/3:

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

For more info, please refer to the documentation:

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

like image 146
Patrick Oscity Avatar answered Sep 28 '22 11:09

Patrick Oscity