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


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


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


this can be done with constraints or triggers in mysql.


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:

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

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..



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

Leave a comment