String as Primary Key

Sometimes, we want to set a primary key of a database table to be a string instead of integer. This is perfectly OK though not best.

However, seems this is not possible when creating tables by using ruby on rails. This post helps a lot.

The following is the sample code:

create_table :employees, {:id => false} do |t|
t.string :emp_id
t.string :first_name
t.string :last_name
end
execute "ALTER TABLE employees ADD PRIMARY KEY (emp_id);"

Comments:

Post a Comment:
Comments are closed for this entry.
About

gc

Search

Categories
Archives
« July 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today