migrations in rails (sqlite) -


hey have small problem rake

class createevents < activerecord::migration    def self.up     create_table :events |t|       t.integer :broadcast_id       t.integer :position       t.string :title       t.string :location       t.string :link       t.text :description       t.datetime :time   end     add_foreign_key :events, :broadcast_id, :broadcasts   end    def self.down     remove_foreign_key :events, :broadcast_id, :broadcasts     drop_table :events   end  end 

problem => add_foreign_key :events, :broadcast_id, :broadcasts

$ rake db:migrate

== createevents: migrating =================================================== -- create_table(:events) -> 0.0021s -- add_index(:events, :broadcast_id) -> 0.0004s rake aborted! error has occurred, , later migrations canceled:  sqlite3::sqlexception: near "foreign": syntax error: alter table "events" add foreign key ("broadcast_id") references "broadcasts"(id) 

why defining foreign keys way?

if want relationship between events , broadcasts should creating active record relation. like

# event model class event < activerecord::base   belongs_to :broadcast end  # broadcast model class broadcast < activerecord::base   has_many :events end 

this way let rails maintain foreign key relationship you. check out rails guide on active record associations more info.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -