Sha256: 6725759589f8adfad45919fc12c339efd3ab5e20fb11bd7ee45872912d42ba82
Contents?: true
Size: 1.02 KB
Versions: 1
Compression:
Stored size: 1.02 KB
Contents
module RC module ForeignKey def add_foreign_key_constraint(referencing_table, referenced_table, options = {}) referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id" referenced_attribute = "id" constraint_name = options[:name] || "#{referencing_attribute}_fk" sql = "ALTER TABLE #{referencing_table} ADD CONSTRAINT #{constraint_name} FOREIGN KEY (#{referencing_attribute}) REFERENCES #{referenced_table} (#{referenced_attribute})" sql << " ON DELETE #{referential_action(options[:on_delete])}" if options[:on_delete] sql << " ON UPDATE #{referential_action(options[:on_update])}" if options[:on_update] execute(sql) end private def referential_action(action) case action.to_sym when :cascade "CASCADE" when :restrict "RESTRICT" when :nullify "SET NULL" when :default "SET DEFAULT" else raise "Unknown referential action '#{action}'" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rein-0.2.0 | lib/rein/constraint/foreign_key.rb |