Giter Club home page Giter Club logo

dm-constraints's Introduction

dm-constraints

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.

Constraints

- :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!]

Examples

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

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

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

# 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
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.