DataMapper plugin constraining relationships
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib Remove jeweler Nov 15, 2011
spec Add backports to spec helpers Apr 9, 2012
tasks Updated jeweler dep to 1.5.2 Feb 18, 2011
LICENSE Updated Copyright year Mar 1, 2011
README.rdoc Stripped whitespace Sep 1, 2011
Rakefile Remove jeweler Nov 15, 2011
dm-constraints.gemspec Remove jeweler Nov 15, 2011



Plugin that adds foreign key constraints to associations. Currently supports only PostgreSQL and MySQL

All constraints are added to the underlying database, but constraining is implemented in pure ruby.


- :protect     returns false on destroy if there are child records
- :destroy     deletes children if present
- :destroy!    deletes children directly without instantiating the resource, bypassing any hooks
               Does not support 1:1 Relationships as #destroy! is not supported on Resource in dm-master
- :set_nil     sets parent id to nil in child associations
               Not valid for M:M relationships as duplicate records could be created (see explanation in specs)
- :skip        Does nothing with children, results in orphaned records

By default a relationship will PROTECT its children.

Cardinality Notes

* 1:1
 * Applicable constraints: [:set_nil, :skip, :protect, :destroy]

* 1:M
 * Applicable constraints: [:set_nil, :skip, :protect, :destroy, :destroy!]

* M:M
 * Applicable constraints: [:skip, :protect, :destroy, :destroy!]


# 1:M Example
class Post
  has n, :comments
  # equivalent to:
  # has n, :comments, :constraint => :protect

# M:M Example
class Article
  has n, :tags, :through => Resource, :constraint => :destroy

class Tags
  has n, :articles, :through => Resource, :constraint => :destroy

# Intermediary constraints for relationships using :through => Resource
# are automatically inherited from the M:M relationship.

# 1:1 Example
class User
  has 1, :address, :constraint => :protect