Re: DRY for ActiveRecord w/PostgreSQL : defaults and validations

Lugovoi Nikolai
Mon, 19 Sep 2005 14:25:30 -0000

Alex Young wrote:
 > Going the other way, it would be *extremely* nice if the DB wrappers
 > could push AR-defined constraints down to the DB level if it detected
 > that they were available (ALTER TABLE... et al), but signal back that
 > they were not if necessary, so that AR could handle it at the
 > application level for those running on a feature-poor backend.
 >

Basically, there are two choices where to put business logic and how
much of it will go to DB or app code (in any case, portions will be
present in both), and the answer to "So, what''s right, what to
choose?"
is simple: "it depends" :)

Agile tools could provide options to inject constraints from DB schema
to ORM-based model and vice versa, and developer can choose where to
take the gun to shoot himself.

The rule is simple: for one application there has to be single data
model, and therefore single place to put constraints (if your model
isn''t constrained, you must be The God or an ignorant idiot :).

So, if one''s model is primarily an Object-Oriented one, constraints are
defined and handled by classes and methods, and if data model grounds
mainly, say, on relational theory, constraints are defined and handled
by RDBMS (yes, we don''t yet have one, only SQL DBMS ;-( ).

My view of validations - keep software more fool-proof (but that''s the
primary goal of constraint) and more luser-friendly.

> Am I totally off-base here?  I can''t help but think that being
able to
> say :dependent in AR is far clearer than having to say ''FOREIGN
KEY...''
> in SQL, in part because you''re then defining the cascade in the
same
> place as the relationship itself, and with a possible push-down from the
> wrapper layer, you get the best of both worlds.
>

Clearer? Maybe, as for developer.
More reliable? I doubt, unless such constraints are pushed from AR to
DB. Data survives apps and often is reused through different apps,
unless one isn''t aware.

And, as for me, it''s rather  scary to develop serious apps against poor
DBMS, though even SQLite can be appropriate sometimes.

WBR, Lugovoi Nikolai