ruby on rails - SQLite3::SQLException while trying to set foreign keys? -
ruby on rails - SQLite3::SQLException while trying to set foreign keys? -
i have 3 models: user, micropost, , comment. i'm trying set foreign keys follows:
class createcomments < activerecord::migration def alter create_table :comments |t| t.text :content t.timestamps end add_index :comments, :micropost_id, :user_id end end
but error:
an error has occurred, , later migrations canceled:
sqlite3::sqlexception: near "user_id": syntax error: create user_id index "index_comments_on_micropost_id" on "comments" ("micropost_id")
i understand rails insert foreign keys based on belongs_to
, has_many
declarations in models. have set:
comment.rb:
class comment < activerecord::base belongs_to :micropost belongs_to :user end
micropost.rb:
class micropost < activerecord::base attr_accessible :title, :content belongs_to :user has_many :comments end
user.rb:
class user < activerecord::base # include default devise modules. others available are: # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # setup accessible (or protected) attributes model attr_accessible :email, :password, :password_confirmation, :remember_me has_many :microposts has_many :comments end
any suggestions prepare this?
if want create index on 2 columns, syntax add_index table_name, [column1_name, column2_name], options
. need define columns in table (activerecord not add together them automatically when add together belongs_to
in model class). migration should be
class createcomments < activerecord::migration def alter create_table :comments |t| t.text :content t.integer :micropost_id t.integer :user_id t.timestamps end add_index :comments, [:micropost_id, :user_id] end end
ruby-on-rails
Comments
Post a Comment