[Django]-Is it possible to create 2 fields that may NOT have the same value?

3👍

In Django, you can use “unique_together” in your model.

http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

unique_together = ((“owner”, “taker”),)

2👍

this can be done with constraints or triggers in mysql.

http://forge.mysql.com/wiki/Triggers#Emulating_Check_Constraints

you would want to ensure the condition owner_id <> taker_id is always enforced.

this is probably better done on the app side, because you probably want to send an appropriate message immediately before starting up the task that assigns a ticket (alerts, status, and whatnot)…

in mysql, this could be done w/something like:

DELIMITER $$
create trigger self_assign_check before insert on helpdesk.ticket for each row
begin
  if new.taker_id = new.owner_id then
     call fail(’Owner cannot take his own ticket’);
  end if;
end $$
DELIMITER ;

note it will have a (in many scenarios a negligible) effect on inserts. fail is a helper procedure described in the wiki that raises a duplicate key error..

👤jspcal

0👍

as mysql do not support CHECK constraint i suggest you to use a trigger if you want that on db level

Leave a comment