我正在尝试将user:references
添加到我已有的模型中。这是我最初写的:
rails g model Post title:string description:text
我这样做是为了通过运行rails generate migration add_user_to_posts user:references
添加用户:引用,我在运行rake db:migrate
时收到此错误:
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists
我正在阅读错误,我知道我已经有了一个User模型,但是,我想将此属性添加到Post模型,而不是User模型。
Db文件:
文章:
class CreatePosts < ActiveRecord::Migration[5.0]
def change
create_table :posts do |t|
t.string :title
t.text :description
t.timestamps
end
end
end
尝试将用户添加到帖子中:
class AddUserToPosts < ActiveRecord::Migration[5.0]
def change
add_reference :posts, :user, foreign_key: true
end
end
用户:
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :name
t.string :uid
t.string :avatar_url
t.timestamps
end
add_index :users, :uid
end
end
但是,rake db:migrate会给我上面的错误。