1

我有一个非常简单的迁移:

class RemoveAuthorIdFromBooks < ActiveRecord::Migration
  def change
    remove_column :books, :author_id
  end

end

但我收到以下错误:

Mysql2::Error: Error on rename of './mysite_staging/#sql-3b1_3c78' to './mysite_staging/books' (errno: 150): ALTER TABLE `books` DROP `author_id`

这是表的描述:

+------------------+---------------+------+-----+---------+----------------+
| Field            | Type          | Null | Key | Default | Extra          |
+------------------+---------------+------+-----+---------+----------------+
| id               | int(11)       | NO   | PRI | NULL    | auto_increment |
| author_id        | int(11)       | NO   | MUL | NULL    |                |
| title            | varchar(255)  | NO   |     | NULL    |                |
| teaser           | varchar(500)  | NO   |     | NULL    |                |
| description      | varchar(2000) | YES  |     | NULL    |                |
| cover_image      | varchar(255)  | NO   |     | NULL    |                |
| publication_date | date          | NO   |     | NULL    |                |
| enabled          | tinyint(1)    | NO   |     | 1       |                |
| created_at       | datetime      | NO   |     | NULL    |                |
| updated_at       | datetime      | NO   |     | NULL    |                |
| excerpt          | text          | YES  |     | NULL    |                |
| featured         | tinyint(1)    | YES  |     | NULL    |                |
| site_id          | int(11)       | YES  |     | NULL    |                |
+------------------+---------------+------+-----+---------+----------------+

有什么线索吗?

4

2 回答 2

6

如果有人仍然面临 Rails 4 及更高版本的问题,那么您可以执行以下操作

remove_reference(:books, :author, index: true, foreign_key: true)

于 2016-02-19T07:12:32.607 回答
1

由于某种原因,外键约束破坏了 drop 语句。

我做了以下事情:

show create table books;

查看了外键名称,然后:

alter table books drop foreign key books_ibfk_1;

然后 rake db:migrate 工作。

于 2013-02-14T20:41:48.773 回答