ruby创建表如何自定义主键

悬赏:5 发布时间:2008-07-14 提问人:vwangzhen (初级程序员)

class CreateBlogs < ActiveRecord::Migration
  def self.up
    create_table :blogs do |t|
      t.references :role   #连接的外键表
      t.string :name,  :limit => 40;  #最在长度是40
      t.decimal :length,:precision => 6, :scale => 2 #同于 decimal(6,2)
      t.string :author,  :default => 'Anonymous coward' #默认值是 Anonymous coward
      t.timestamp :created_on
      t.timestamp :updated_on
      t.text :description
    end
    add_column "articles" :integer  #添加列
  end

  def self.down
    drop_table :blogs
    remove_column "articles", "blog_id"
  end

问题补充:
自定义主键也就是说如登陆用的 userid 如果用userid 做主键 怎么做

create_table :blogs :primary_key => primaryId do |t|  怎么来定义它是字符串的

采纳的答案

2008-07-15 superxielei (初级程序员)

create_table :blogs :primary_key => primaryId do |t|

提问者对于答案的评价:
它生成主键盘的方式 和id 差不多我的博客有解释在这不多说了

其他回答

自定义主键??不是 很明白
在rails中
默认的  id就是主键
rails  是不允许对主键操作的
yuanzy (初级程序员) 2008-07-14