On Sep 2, 7:45 pm, Dave Howell <[email protected]> wrote:
> Sequel and I aren't agreeing as to what constitutes validity.
>
> create table datathings (
>         id uuid DEFAULT uuid_generate_v1() NOT NULL,
>         singing varchar not null,
>         dancing varchar,
>         primary key (id)
> )
>
> class Datathing <  < Sequel::Model        
> end
>
>  >> d = Datathing.new
>  => #< Datathing @values={}>
>  >> d.save
>  Sequel::DatabaseError: PGError: ERROR:  null value in column "singing" 
> violates not-null constraint
>  >> d.valid?
>  => true
>
> I'm sorry, but "true" is not the correct answer. I started to try to find a 
> way for Sequel to learn what the database's constraints were, but I 
> discovered that it already knows them.
>
>  >> d.singing = "Girls Just Want To Have Fun"
>  => "Girls Just Want To Have Fun"
>  >> d
>  => #< Datathing @values={:singing=>"Girls Just Want To Have Fun"}>
>  >>d.singing = nil
> Sequel::InvalidValue: nil/NULL is not allowed for the singing column
>
> So if Sequel already *knows* that singing cannot be null, why would .valid? 
> return 'true' when I first created a new Datathing object and singing hadn't 
> been initialized yet??

Sequel does not parse database constraints.  The reason you get that
error (a typecasting error, not a validation error), is that the NULL/
NOT NULL information is determined when parsing the column schema for
the table (for use in typecasting).

Validations are always specified by the user, and exist solely to
provide nice error messages to the user (often on websites).
Generally, if you are using them, you'll want to set
"raise_on_typecast_failure = false" for your models, and set up
validations for all of the columns in the model.  If you are writing
code that doesn't interface with the user, you probably don't want to
use validations.  If you are writing code that interfaces with the
user, you'll want to add a validate method to your models and handle
validation there.

See http://sequel.rubyforge.org/rdoc/files/doc/validations_rdoc.html
for more details on validations.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to