[英]Rake aborted due to Migration error
我正在更改以前的关注者模型的名称。 我决定回到原来的名字,但是遇到了错误。 我决定使用Drop_table方法解决该问题,但进一步寻求解决。 我删除了与关注者相关的所有文件,并进行了db:schema:load倾斜处理,这没有导致任何错误。 但是每当我尝试做
rails g model Follower follower_id:integer followed_id:integer
并将其编辑为
def change
create_table :followers do |t|
t.integer :follower_id
t.integer :followed_id
t.timestamps
end
add_index :followers, :follower_id
add_index :followers, :leader_id
add_index :followers, [:follower_id, :leader_id], unique: true
end
然后我执行rake db:migrate
并得到以下信息。 任何人都知道我将如何解决此问题而不必从头开始? 任何帮助表示赞赏。 谢谢。
== 20150910203914 CreateFollowers: migrating ==================================
-- create_table(:followers)
-> 0.0011s
-- add_index(:followers, :follower_id)
-> 0.0004s
-- add_index(:followers, :leader_id)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table followers has no column named leader_id: CREATE INDEX "index_followers_on_leader_id" ON "followers" ("leader_id")/Users/Steven/.rvm/gems/ruby-2.1.3/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize'
如错误所述,您没有名为leader_id
的列。 您应该将迁移更改为:
def change
create_table :followers do |t|
t.integer :follower_id
t.integer :leader_id
t.timestamps
end
add_index :followers, :follower_id
add_index :followers, :leader_id
add_index :followers, [:follower_id, :leader_id], unique: true
end
或更确切地说,将leader_id
更改为followed_id
。
您还应该在activerecord迁移中检查references
修饰符( source )。
您的表没有一列leader_id
。 您会看到这是因为您正在迁移中创建表,并且仅指定3列,而没有指定leader_id
。 但是您正在尝试使用该列添加索引,这就是产生错误的原因。 如果您不再需要该列,则需要更改索引。 如果您确实想要它,请按照jihonora的建议进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.