Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off auto_increment in Rails Active Record

Is it possible to create primary key without auto_increment flag in ActiveRecord?

I can't do

create table :blah, :id => false

because I want to have primary key index on the column. I looked up documentation but didn't find anything useful.

Is it possible to create primary key without auto_increment?

like image 886
Jakub Arnold Avatar asked Oct 18 '09 16:10

Jakub Arnold


People also ask

How do I turn on auto increment?

Syntax for Access Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change the autoincrement to AUTOINCREMENT(10,5) . VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table. The "Personid" column would be assigned a unique value.

How do I remove Autoincrement?

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

Is primary key always auto increment?

A primary key is by no means required to use the auto_increment property - it just needs to be a unique, not-null, identifier, so the account number would do just fine.

Is auto increment a constraint?

Auto increment is used with the INT data type. The INT data type supports both signed and unsigned values. Unsigned data types can only contain positive numbers. As a best practice, it is recommended to define the unsigned constraint on the auto increment primary key.


1 Answers

Try this?

create_table(:table_name, :id => false) do |t|
  t.integer :id, :options => 'PRIMARY KEY'
end
like image 86
Jim Avatar answered Sep 19 '22 18:09

Jim