Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Migration Create Table Primary Key

I am writing a migration script to create a table with a primary key column that is named guid and is a VARCHAR(25). The issue is I feel like I am having to double my effort to achieve what should be possible in one step.

If I run:

create_table(:global_feeds, :primary_key => 'guid') do |t|
  t.string :guid, :limit => 25
  t.text :title
  t.text :subtitle

  ...

  t.timestamps
end

I get a table with a primary key called guid no column called id (which is what I want). However, the issue is the guid column is an INT(11) with auto increment turned on. So I have to run one additional command:

change_column :global_feeds, :guid, :string, :limit => 25

Seems a bit convoluted to have to basically run two SQL commands to get what I believe should be possible in one.

Any suggestions on how to optimize this?

like image 281
Ryan Avatar asked Jan 22 '11 18:01

Ryan


2 Answers

In Rails 4 you can do;

create_table :global_feeds, id: false do |t|
  t.string :guid, primary_key: true
 ...
end
like image 161
kakoni Avatar answered Oct 05 '22 22:10

kakoni


I suppose you're using mySQL, so here is what you can try

create_table :global_feeds, {:id => false} do |t|
  t.string :guid
  t.text :title
  t.text :subtitle
  t.timestamps
end
execute "ALTER TABLE global_feeds ADD PRIMARY KEY (guid);"

If you're not on mySQL you should change the query in the execute command to work on your DB engine. I'm not sure if you can do that on SQLite though. And don't forget to put this line somewhere in global_feed.rb model:

set_primary_key :guid

Anyway you're getting the most out of Rails while you're sticking to its conventions. Changing primary key name and type might not be a very good idea.

like image 28
Max Al Farakh Avatar answered Oct 05 '22 22:10

Max Al Farakh