Mysql 编辑rails迁移表不需要';不影响生成的表

Mysql 编辑rails迁移表不需要';不影响生成的表,mysql,ruby-on-rails,ruby,ruby-on-rails-4,Mysql,Ruby On Rails,Ruby,Ruby On Rails 4,我意识到我不正确地生成了一个模型,并且在“回滚”错误时遇到了麻烦。我正在生成一个模型以包含父窗体的问题,并将其编写如下: class CreateQuestions < ActiveRecord::Migration def change create_table :questions do |t| t.integer :legalform_id t.integer :question_number t.string :type t.text :the_questio

我意识到我不正确地生成了一个模型,并且在“回滚”错误时遇到了麻烦。我正在生成一个模型以包含父窗体的问题,并将其编写如下:

class CreateQuestions < ActiveRecord::Migration
  def change
create_table :questions do |t|
  t.integer  :legalform_id
  t.integer :question_number
  t.string :type
  t.text :the_question

  t.timestamps
end
  end
end
mysql> show columns in questions;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| legalform_id    | int(11)      | YES  |     | NULL    |                |
| question_number | int(11)      | YES  |     | NULL    |                |
| type            | varchar(255) | YES  |     | NULL    |                |
| the_question    | text         | YES  |     | NULL    |                |
| created_at      | datetime     | YES  |     | NULL    |                |
| updated_at      | datetime     | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
我输入了命令
rakedb:rollback
,该命令删除了问题表,然后运行
rakedb:migrate
,假设我对迁移表所做的更改将相应地生成mysql表。令我惊讶的是,新表包含与原始迁移表读取时指定的属性相同的属性,如下所示:

class CreateQuestions < ActiveRecord::Migration
  def change
create_table :questions do |t|
  t.integer  :legalform_id
  t.integer :question_number
  t.string :type
  t.text :the_question

  t.timestamps
end
  end
end
mysql> show columns in questions;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| legalform_id    | int(11)      | YES  |     | NULL    |                |
| question_number | int(11)      | YES  |     | NULL    |                |
| type            | varchar(255) | YES  |     | NULL    |                |
| the_question    | text         | YES  |     | NULL    |                |
| created_at      | datetime     | YES  |     | NULL    |                |
| updated_at      | datetime     | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

这可能是什么原因?我错过什么了吗?非常感谢您的指导。

这正是您想要的。它创建相同的列,同时添加索引
add_reference
使用指定的名称创建列,在您的情况下,该名称与原始迁移相同。如果在表上运行
SHOW INDEXES
,则还应该看到新创建的索引

引用
也会创建相同的外键约束,在您的情况下
legalform_id
,使用
references
legalform_id
之间的区别在于,当使用
references
时,它将自动在模型中添加适当的关系定义,并为字段添加索引

Hey Brennan,感谢您的回复。不过我有点困惑。如果发生了更改,我是否会有一个名为
legalform
的列,并且没有列
legalform\u id
?否。Rails知道引用是指向外部表的。它将
\u id
附加到列的名称,因为它知道您将使用该列引用另一个表。我猜rails开发团队和我都有类似的命名约定。非常感谢!。它不仅仅是rails,它是一种广泛使用的数据库约定。如果您创建迁移并指定它使用引用,它确实可以。