Ruby 1.8.7-p249, Rails 2.3.8, SQLite3.
Я хотел бы создать две таблицы программно и определить отношения между родителями и детьми. Метод foreign_key работает в миграциях БД, но в моем коде в блоке ActiveRecord :: Schema.define он не работает
Как создать внешний ключ программно?
Код:
# To change this template, choose Tools | Templates
# and open the template in the editor.
require "rubygems"
require "active_record"
require "logger"
require "pp"
ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "../db/development_mp.sqlite3"
)
ActiveRecord::Schema.define do
create_table :orders, :force => true do |t|
t.string :name
t.timestamps
end
create_table :invoices, :force => true do |t|
t.integer :order_id
t.timestamps
end
# Error!
foreign_key(:invoices, :order_id, :orders)
end
Ошибка:
-- foreign_key(:invoices, :order_id, :orders) D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/migration.rb:352:in `send': undefined method `foreign_key' for
#<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0x554f2f8> (NoMethodError)
from D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/migration.rb:352:in `method_missing'
from D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/migration.rb:328:in `say_with_time'
from D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
from D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/migration.rb:328:in `say_with_time'
from D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/migration.rb:348:in `method_missing'
from D:/prg/Ruby/Pragmatic.Agile-Web-Development-With-Rails-Third-Edition/active_record_basics/lib/has_one_mp.rb:29
from D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/schema.rb:47:in `instance_eval'
from D:/Ruby/bitnami-rubystack-2.1-0/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/schema.rb:47:in `define'
from D:/prg/Ruby/Pragmatic.Agile-Web-Development-With-Rails-Third-Edition/active_record_basics/lib/has_one_mp.rb:16