【问题标题】:Need other than id as a primary key in rails active record migration script在rails活动记录迁移脚本中需要id以外的主键
【发布时间】:2014-10-09 09:25:01
【问题描述】:

我有一张桌子叫学生

mysql> desc students;
+--------------------+---------------+------+-----+---------+----------------+
| Field              | Type          | Null | Key | Default | Extra          |
+--------------------+---------------+------+-----+---------+----------------+
| id                 | int(11)       | NO   | PRI | NULL    | auto_increment |
| height             | decimal(15,8) | YES  |     | NULL    |                |
| name               | varchar(255)  | YES  |     | NULL    |                |
| text               | varchar(255)  | YES  |     | NULL    |                |
| status             | tinyint(4)    | YES  |     | NULL    |                |
| created_at         | datetime      | YES  |     | NULL    |                |
| updated_at         | datetime      | YES  |     | NULL    |                |
+--------------------+---------------+------+-----+---------+----------------+

我想创建一个名为 students_data 的新表,其中 student_name 作为外键,name 作为来自 students 表的主键。我应该如何更改我的脚本?以下脚本将 id 作为学生表中的主键

class CreateStudentsData < ActiveRecord::Migration
  def change
    create_table :students_data do |t|
      t.belongs_to :students, :foreign_key => :student_name, :primary_key => :name
    end
  end
end

我应该如何更改我的脚本以获取名称作为主键?

注意:我不想像建议的 here 那样将 primary_key id 设置为 false。我需要 id 和 name 作为主键。

【问题讨论】:

  • 只是为了兴趣:你为什么需要它?为什么不在 student_data 表中使用student_id
  • @IS04 - 我的经理要求

标签: mysql sql ruby-on-rails migration rails-migrations


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 2013-09-11
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多