Module: Fakey::PostgreSQLAdapter
- Defined in:
- lib/fakey/postgresql_adapter.rb
Instance Method Summary collapse
-
#add_column_with_foreign_key(table_name, column_name, type, options = {}) ⇒ Object
Adds a new column to the named table.
- #add_foreign_key(to_table, column, options = {}) ⇒ Object
Instance Method Details
#add_column_with_foreign_key(table_name, column_name, type, options = {}) ⇒ Object
Adds a new column to the named table. See TableDefinition#column for details of the options you can use.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/fakey/postgresql_adapter.rb', line 14 def add_column_with_foreign_key(table_name, column_name, type, = {}) default = [:default] notnull = [:null] == false foreign_key = "REFERENCES #{quote_table_name(options[:references])}" foreign_key +="(#{quote_column_name(options[:referenced_column] || options[:primary_key])})" if [:referenced_column] || [:primary_key] # Add the column. execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])} #{foreign_key}") change_column_default(table_name, column_name, default) if () change_column_null(table_name, column_name, false, default) if notnull end |
#add_foreign_key(to_table, column, options = {}) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/fakey/postgresql_adapter.rb', line 4 def add_foreign_key(to_table, column, ={}) to_table = [:references] if [:references] res = "FOREIGN KEY(#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)}" referenced_column = [:referenced_column] || [:primary_key] res += "(#{referenced_column})" if referenced_column res end |