Django UNIQUE constraint












0















Django UNIQUE constraint failed error



django.db.utils.IntegrityError: UNIQUE constraint failed while I am using the unique together constraint in the models.



How can I use it properly?
While I am entering the data for the same publisher same issue same version number but different copy number I am getting the above error.
Is this the correct way to implement it??



class Book(models.Model):
publisher = models.IntegerField()
issue = models.CharField(max_length=255)

class Meta:
unique_together = ("publisher", "issue")

def __str__(self):
return "%s-%r" % (self.publisher,self.issue)


class Version(models.Model):
book = models.ForeignKey(Book, on_delete = models.CASCADE)
number = models.CharField(max_length=255)
book_name = models.CharField(max_length=255)
cover = models.ImageField(null=True)
description = models.CharField(max_length=255)

class Meta:
unique_together = ("book", "number")

def __str__(self):
return "%s %s" % (self.number, self.book)




class Copy(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
version = models.ForeignKey(Version, on_delete = models.CASCADE)
number = models.IntegerField()
book_id = models.AutoField(primary_key=True)
book_size = models.CharField(null=True, blank=True,max_length=255)


def __str__(self):
return "%r" % (self.book_id)


class Meta:
unique_together = ("version", "number")









share|improve this question

























  • When did this error occured? While migrating?

    – a_k_v
    Nov 28 '18 at 9:36











  • While posting my data after executing the migration

    – kkr
    Nov 28 '18 at 9:37











  • It cause because duplicate data already exist in your database!!!

    – a_k_v
    Nov 28 '18 at 9:49











  • I have removed the data and tried. The thing is I am having three different models and the unique together relationships are constructed across the the three tables. Is it possible to implement that?

    – kkr
    Nov 28 '18 at 9:51











  • Post some more code. Your model structure etc

    – a_k_v
    Nov 28 '18 at 9:53
















0















Django UNIQUE constraint failed error



django.db.utils.IntegrityError: UNIQUE constraint failed while I am using the unique together constraint in the models.



How can I use it properly?
While I am entering the data for the same publisher same issue same version number but different copy number I am getting the above error.
Is this the correct way to implement it??



class Book(models.Model):
publisher = models.IntegerField()
issue = models.CharField(max_length=255)

class Meta:
unique_together = ("publisher", "issue")

def __str__(self):
return "%s-%r" % (self.publisher,self.issue)


class Version(models.Model):
book = models.ForeignKey(Book, on_delete = models.CASCADE)
number = models.CharField(max_length=255)
book_name = models.CharField(max_length=255)
cover = models.ImageField(null=True)
description = models.CharField(max_length=255)

class Meta:
unique_together = ("book", "number")

def __str__(self):
return "%s %s" % (self.number, self.book)




class Copy(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
version = models.ForeignKey(Version, on_delete = models.CASCADE)
number = models.IntegerField()
book_id = models.AutoField(primary_key=True)
book_size = models.CharField(null=True, blank=True,max_length=255)


def __str__(self):
return "%r" % (self.book_id)


class Meta:
unique_together = ("version", "number")









share|improve this question

























  • When did this error occured? While migrating?

    – a_k_v
    Nov 28 '18 at 9:36











  • While posting my data after executing the migration

    – kkr
    Nov 28 '18 at 9:37











  • It cause because duplicate data already exist in your database!!!

    – a_k_v
    Nov 28 '18 at 9:49











  • I have removed the data and tried. The thing is I am having three different models and the unique together relationships are constructed across the the three tables. Is it possible to implement that?

    – kkr
    Nov 28 '18 at 9:51











  • Post some more code. Your model structure etc

    – a_k_v
    Nov 28 '18 at 9:53














0












0








0








Django UNIQUE constraint failed error



django.db.utils.IntegrityError: UNIQUE constraint failed while I am using the unique together constraint in the models.



How can I use it properly?
While I am entering the data for the same publisher same issue same version number but different copy number I am getting the above error.
Is this the correct way to implement it??



class Book(models.Model):
publisher = models.IntegerField()
issue = models.CharField(max_length=255)

class Meta:
unique_together = ("publisher", "issue")

def __str__(self):
return "%s-%r" % (self.publisher,self.issue)


class Version(models.Model):
book = models.ForeignKey(Book, on_delete = models.CASCADE)
number = models.CharField(max_length=255)
book_name = models.CharField(max_length=255)
cover = models.ImageField(null=True)
description = models.CharField(max_length=255)

class Meta:
unique_together = ("book", "number")

def __str__(self):
return "%s %s" % (self.number, self.book)




class Copy(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
version = models.ForeignKey(Version, on_delete = models.CASCADE)
number = models.IntegerField()
book_id = models.AutoField(primary_key=True)
book_size = models.CharField(null=True, blank=True,max_length=255)


def __str__(self):
return "%r" % (self.book_id)


class Meta:
unique_together = ("version", "number")









share|improve this question
















Django UNIQUE constraint failed error



django.db.utils.IntegrityError: UNIQUE constraint failed while I am using the unique together constraint in the models.



How can I use it properly?
While I am entering the data for the same publisher same issue same version number but different copy number I am getting the above error.
Is this the correct way to implement it??



class Book(models.Model):
publisher = models.IntegerField()
issue = models.CharField(max_length=255)

class Meta:
unique_together = ("publisher", "issue")

def __str__(self):
return "%s-%r" % (self.publisher,self.issue)


class Version(models.Model):
book = models.ForeignKey(Book, on_delete = models.CASCADE)
number = models.CharField(max_length=255)
book_name = models.CharField(max_length=255)
cover = models.ImageField(null=True)
description = models.CharField(max_length=255)

class Meta:
unique_together = ("book", "number")

def __str__(self):
return "%s %s" % (self.number, self.book)




class Copy(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
version = models.ForeignKey(Version, on_delete = models.CASCADE)
number = models.IntegerField()
book_id = models.AutoField(primary_key=True)
book_size = models.CharField(null=True, blank=True,max_length=255)


def __str__(self):
return "%r" % (self.book_id)


class Meta:
unique_together = ("version", "number")






django django-models unique-constraint






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 10:03







kkr

















asked Nov 28 '18 at 9:34









kkrkkr

308




308













  • When did this error occured? While migrating?

    – a_k_v
    Nov 28 '18 at 9:36











  • While posting my data after executing the migration

    – kkr
    Nov 28 '18 at 9:37











  • It cause because duplicate data already exist in your database!!!

    – a_k_v
    Nov 28 '18 at 9:49











  • I have removed the data and tried. The thing is I am having three different models and the unique together relationships are constructed across the the three tables. Is it possible to implement that?

    – kkr
    Nov 28 '18 at 9:51











  • Post some more code. Your model structure etc

    – a_k_v
    Nov 28 '18 at 9:53



















  • When did this error occured? While migrating?

    – a_k_v
    Nov 28 '18 at 9:36











  • While posting my data after executing the migration

    – kkr
    Nov 28 '18 at 9:37











  • It cause because duplicate data already exist in your database!!!

    – a_k_v
    Nov 28 '18 at 9:49











  • I have removed the data and tried. The thing is I am having three different models and the unique together relationships are constructed across the the three tables. Is it possible to implement that?

    – kkr
    Nov 28 '18 at 9:51











  • Post some more code. Your model structure etc

    – a_k_v
    Nov 28 '18 at 9:53

















When did this error occured? While migrating?

– a_k_v
Nov 28 '18 at 9:36





When did this error occured? While migrating?

– a_k_v
Nov 28 '18 at 9:36













While posting my data after executing the migration

– kkr
Nov 28 '18 at 9:37





While posting my data after executing the migration

– kkr
Nov 28 '18 at 9:37













It cause because duplicate data already exist in your database!!!

– a_k_v
Nov 28 '18 at 9:49





It cause because duplicate data already exist in your database!!!

– a_k_v
Nov 28 '18 at 9:49













I have removed the data and tried. The thing is I am having three different models and the unique together relationships are constructed across the the three tables. Is it possible to implement that?

– kkr
Nov 28 '18 at 9:51





I have removed the data and tried. The thing is I am having three different models and the unique together relationships are constructed across the the three tables. Is it possible to implement that?

– kkr
Nov 28 '18 at 9:51













Post some more code. Your model structure etc

– a_k_v
Nov 28 '18 at 9:53





Post some more code. Your model structure etc

– a_k_v
Nov 28 '18 at 9:53












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516256%2fdjango-unique-constraint%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516256%2fdjango-unique-constraint%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks