Module: SchemaPlus::ForeignKeys::Middleware::Sql::Table

Defined in:
lib/schema_plus/foreign_keys/middleware/sql.rb

Defined Under Namespace

Modules: SQLite3

Instance Method Summary collapse

Instance Method Details

#after(env) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/schema_plus/foreign_keys/middleware/sql.rb', line 6

def after(env)
  foreign_keys = if env.table_definition.foreign_keys.is_a? Array
                   env.table_definition.foreign_keys
                 else
                   env.table_definition.foreign_keys.values.tap { |v| v.flatten! }
                 end

  # create foreign key constraints inline in table definition
  env.sql.body = ([env.sql.body] + foreign_keys.map(&:to_sql)).join(', ')

  # prevents AR >= 4.2.1 from emitting add_foreign_key after the table
  env.table_definition.foreign_keys.clear
end