newBike
newBike

Reputation: 15002

How to change the column's position in table

If I have a table User.

At first, it has 2 columns phone, address

Later I need to a an new column name,

How could I add the new column and put the new column in first position.

I know how to add a new column

add_column :users, :name, :string

But not knowing how to change its order.

Because it will append to the last position by default.

I read a book 'head first SQL'

It told me , it's a better practice to put your primary key in the first position

Upvotes: 0

Views: 237

Answers (2)

Choco
Choco

Reputation: 1064

You can use :after in rails to position your columns like this:

add_column :users, :name, :string, :after => :id #primary_key

Upvotes: 0

karlingen
karlingen

Reputation: 14645

Use the :first option

add_column :users, :name, :string, :first => true

Upvotes: 1

Related Questions