constraint unique with models.Model

23 views
Skip to first unread message

Marc Aymerich

unread,
Dec 27, 2009, 11:08:11 PM12/27/09
to django...@googlegroups.com
hi guys!

I'm writing my first project with django and I have doubts in how to declare a constraint unique for two fields of the same table using models.Model. In MySQL would be something like: 

CREATE TABLE `virtual_aliases` (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
domainid INT(11) NOT NULL,
source VARCHAR(40) NOT NULL,
destination VARCHAR(80) NOT NULL,
CONSTRAINT UNIQUE_EMAIL UNIQUE (domainid,source),
FOREIGN KEY (domainid) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE = InnoDB;

with django I only do that:

class virtual_aliases(models.Model):
domain = models.ForeignKey(virtual_domains)
source = models.CharField(max_length=50)
destination = models.CharField(max_length=60)

I haven't any idea how to declare a unique constraint between 'source' and 'domain'.

Any help¿¿

Thanks a lot!
Marc

Karen Tracey

unread,
Dec 27, 2009, 11:15:50 PM12/27/09
to django...@googlegroups.com
On Sun, Dec 27, 2009 at 11:08 PM, Marc Aymerich <glic...@gmail.com> wrote:
hi guys!

I'm writing my first project with django and I have doubts in how to declare a constraint unique for two fields of the same table using models.Model.

Marc Aymerich

unread,
Dec 27, 2009, 11:27:35 PM12/27/09
to django...@googlegroups.com
Thank you very much Karen!!  :)

If someone serves...

class virtual_aliases(models.Model):
domain = models.ForeignKey(domains)
source = models.CharField(max_length=50)
destination = models.CharField(max_length=60)
class Meta:
unique_together = ("source", "domain")

--

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

Reply all
Reply to author
Forward
0 new messages