List Info

Thread: Check constraint in models




Check constraint in models
user name
2007-12-25 16:34:42
Hello,

Having the following (Postgre)SQL statement:

CREATE TABLE Games (
  minPlayer integer NOT NULL DEFAULT 1,
  maxPlayer integer NOT NULL DEFAULT 1,
  CHECK (min_player <= max_player)
)

the consequent Model for Django would be:

class Game(model.Models):
  min_player = models.IntegerField(default=1),
  max_player = models.IntegerField(default=1)

But how can I define the "CHECK (min_player <=
max_player)"
constraint? I tried overriding the save method to:

def save(self):
  if self.max_player < self.min_player:
    return False
  super(Game, self).save()

and although no game will be created (when specifying
max_player <
min_player), the Admin interface will output a successful
creation to
the user, when it is not.

Workarounds any?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Check constraint in models
country flaguser name
Australia
2007-12-25 21:35:54

On Tue, 2007-12-25 at 14:34 -0800, bruno.jacquetgmail.com
wrote:
> Hello,
> 
> Having the following (Postgre)SQL statement:
> 
> CREATE TABLE Games (
>   minPlayer integer NOT NULL DEFAULT 1,
>   maxPlayer integer NOT NULL DEFAULT 1,
>   CHECK (min_player <= max_player)
> )
> 
> the consequent Model for Django would be:
> 
> class Game(model.Models):
>   min_player = models.IntegerField(default=1),
>   max_player = models.IntegerField(default=1)
> 
> But how can I define the "CHECK (min_player <=
max_player)"
> constraint? I tried overriding the save method to:
> 
> def save(self):
>   if self.max_player < self.min_player:
>     return False
>   super(Game, self).save()
> 
> and although no game will be created (when specifying
max_player <
> min_player), the Admin interface will output a
successful creation to
> the user, when it is not.

This requires model validation to be finished, which is
currently work
in progress. Validation and saving are two different steps
and the
save() method won't raise any validation errors, but it
might raise
database integrity errors (such as when the check constraint
fails). We
will shortly have code that implements validate() on models
(don't get
thrown by the fact there's already a validate() method on
the Model
class; it's incomplete and should not be used) and
possibly/probably
even a shortcut to allow "validate and save" in
one step, but, in any
case, the admin interface will make sure models are valid
before trying
to save them.

At the moment, there's no really good workaround for this
with the
current admin and state of model validation.

Regards,
Malcolm

-- 
Borrow from a pessimist - they don't expect it back. 
http://www.pointy-s
tick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Check constraint in models
user name
2007-12-27 14:23:49
django-check-constraints

On Dec 26, 12:34 am, bruno.jacq...gmail.com wrote:
> Hello,
>
> Having the following (Postgre)SQL statement:
>
> CREATE TABLE Games (
>   minPlayer integer NOT NULL DEFAULT 1,
>   maxPlayer integer NOT NULL DEFAULT 1,
>   CHECK (min_player <= max_player)
> )
>
> the consequent Model for Django would be:
>
> class Game(model.Models):
>   min_player = models.IntegerField(default=1),
>   max_player = models.IntegerField(default=1)
>
> But how can I define the "CHECK (min_player <=
max_player)"
> constraint? I tried overriding the save method to:
>
> def save(self):
>   if self.max_player < self.min_player:
>     return False
>   super(Game, self).save()
>
> and although no game will be created (when specifying
max_player <
> min_player), the Admin interface will output a
successful creation to
> the user, when it is not.
>
> Workarounds any?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Check constraint in models
user name
2007-12-27 14:23:04
consider using django add-on django-check-constraints

http://code.google.com/p/django-check-constraints
/wiki/Features

On Dec 26, 12:34 am, bruno.jacq...gmail.com wrote:
> Hello,
>
> Having the following (Postgre)SQL statement:
>
> CREATE TABLE Games (
>   minPlayer integer NOT NULL DEFAULT 1,
>   maxPlayer integer NOT NULL DEFAULT 1,
>   CHECK (min_player <= max_player)
> )
>
> the consequent Model for Django would be:
>
> class Game(model.Models):
>   min_player = models.IntegerField(default=1),
>   max_player = models.IntegerField(default=1)
>
> But how can I define the "CHECK (min_player <=
max_player)"
> constraint? I tried overriding the save method to:
>
> def save(self):
>   if self.max_player < self.min_player:
>     return False
>   super(Game, self).save()
>
> and although no game will be created (when specifying
max_player <
> min_player), the Admin interface will output a
successful creation to
> the user, when it is not.
>
> Workarounds any?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )