Skip to content

Instantly share code, notes, and snippets.

@konjoot
Created January 29, 2015 12:35
Show Gist options
  • Save konjoot/458ee3e72fe34764d47b to your computer and use it in GitHub Desktop.
Save konjoot/458ee3e72fe34764d47b to your computer and use it in GitHub Desktop.
schema_plus buggy migration
class Migration < ActiveRecord::Migration
def change
SchemaPlus.setup do |config|
config.foreign_keys.auto_create = false
end
create_table :period_types, force: true do |t|
t.string :name
end
create_table :grade_systems, force: true do |t|
t.string :name
t.integer :school_id
t.integer :parent_id
t.integer :profile_id
end
create_table :schools, force: true do |t|
t.string :name
t.integer :default_grade_system_id
end
create_table :academic_years, force: true do |t|
t.string :name
t.integer :school_id
t.integer :period_type_id
end
create_table :buildings, force: true do |t|
t.string :name
t.integer :school_id
end
create_table :publishing_houses, force: true do |t|
t.string :name
end
create_table :profiles, force: true do |t|
t.integer :school_id
t.integer :publishing_house_id
t.integer :building_id
end
create_table :class_units, force: true do |t|
t.string :name
t.integer :school_id
t.integer :mentor_id
t.integer :building_id
end
ActiveRecord::Base.connection.add_foreign_key(School.table_name, :default_grade_system_id, GradeSystem.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(GradeSystem.table_name, :school_id, School.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(GradeSystem.table_name, :parent_id, GradeSystem.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(GradeSystem.table_name, :profile_id, Profile.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(Profile.table_name, :building_id, Building.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(Profile.table_name, :school_id, School.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(ClassUnit.table_name, :school_id, School.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(ClassUnit.table_name, :building_id, Building.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(ClassUnit.table_name, :mentor_id, Profile.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(Building.table_name, :school_id, School.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(AcademicYear.table_name, :school_id, School.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(AcademicYear.table_name, :period_type_id, PeriodType.table_name, :id)
ActiveRecord::Base.connection.add_foreign_key(Profile.table_name, :publishing_house_id, PublishingHouse.table_name, :id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment