LoginSignup
0

More than 5 years have passed since last update.

PadrinoでActiveRecord::Migrationで外部キー制約を追加するforeignerを使う

Posted at

ActiveRecordで外部キー制約を追加するためのプラグインは種々あるみたいですが、foreignerが3.0系にも対応してて、(たぶん) 一番新しい + 開発されている様子がある。

Padrinoで使うにはちょっとしたセットアップが必要だったのでメモ。

config/boot.rb
##
# Add your after load hooks here
#
Padrino.after_load do
  ActiveSupport.on_load :active_record do
    ActiveRecord::ConnectionAdapters.module_eval do
      include Foreigner::ConnectionAdapters::SchemaStatements
      include Foreigner::ConnectionAdapters::SchemaDefinitions
    end

    ActiveRecord::SchemaDumper.module_eval do
      include Foreigner::SchemaDumper
    end

    ActiveRecord::Migration::CommandRecorder.module_eval do
      include Foreigner::Migration::CommandRecorder
    end if defined?(ActiveRecord::Migration::CommandRecorder)

    Foreigner::Adapter.load!
  end
end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0