Skip to content

Commit

Permalink
test and document use of t.references
Browse files Browse the repository at this point in the history
  • Loading branch information
ronen committed Aug 14, 2011
1 parent dcb5887 commit 2aeb86c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.rdoc
Expand Up @@ -92,11 +92,15 @@ Here are some examples:
t.integer :author_id, :references => [:people, :ssn] # override table name and key
t.integer :author_id, :referencs => nil # don't create a constraint


You can also modify the behavior using +:on_delete+, +:on_update+, and +:deferrable+

t.integer :author_id, :on_delete => :cascade

SchemaPlus also creates foreign keys when used rails' <tt>t.references</tt>
or <tt>t.belongs_to</tt>< which take the singular of the referenced table
name and implicitly create the column suffixed with +_id+, and it accepts
the same arguments.

The foreign key behavior can be configured globally (see Config) or per-table (see create_table).

To examine your foreign key constraints, connection.foreign_keys returns a
Expand Down
10 changes: 10 additions & 0 deletions spec/migration_spec.rb
Expand Up @@ -36,6 +36,16 @@
@model.should reference(:users, :id).on(:user_id)
end

it "should create foreign key using t.references" do
create_table(@model, :user => {:METHOD => :references})
@model.should reference(:users, :id).on(:user_id)
end

it "should not create foreign key using t.references with :references => nil" do
create_table(@model, :user => {:METHOD => :references, :references => nil})
@model.should_not reference(:users, :id).on(:user_id)
end

it "should create foreign key to the same table on parent_id" do
create_table(@model, :parent_id => {})
@model.should reference(@model.table_name, :id).on(:parent_id)
Expand Down

0 comments on commit 2aeb86c

Please sign in to comment.