Skip to content

Instantly share code, notes, and snippets.

@cassiomarques
Created November 5, 2010 01:14
Show Gist options
  • Save cassiomarques/663500 to your computer and use it in GitHub Desktop.
Save cassiomarques/663500 to your computer and use it in GitHub Desktop.
How to read constraints direct from postgresql databases
# == Schema Information
#
# Table name: information_schema.table_constraints
#
# constraint_catalog :
# constraint_schema :
# constraint_name :
# table_catalog :
# table_schema :
# table_name :
# constraint_type :string
# is_deferrable :string
# initially_deferred :string
#
class TableConstraint < ActiveRecord::Base
set_table_name 'information_schema.table_constraints'
scope :foreign_keys, where('constraint_type = ?', 'FOREIGN KEY')
scope :primary_keys, where('constraint_type = ?', 'PRIMARY KEY')
def self.exists?(conditions)
self.count(:conditions => conditions) > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment