IntegrityError in TestCase runs after updating to Django 2.0
I upgraded from Django 1.11 to Django 2.0 and my tests started failing. I have 7 TestCase
classes and all use the setUpTestData
provided by Django. When I run them all together one of them fails to set up because of an psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
.
When I run one of those TestCase
classes alone it works fine. It seems like they're influencing each other in some way, but it's strange that it would fail after upgrading to Django 2.0. I've also noticed it's not at the create()
it's at the save()
.
In the setup for the dashboards
app I have some creation data:
cls.d1 = doctor_models.Doctor.objects.create(email="johndoe@example.com", name="John Doe",
specialization=cls.s1, premium=True,
premium_price=4310, consultation_price=341)
...
cls.b1 = doctor_models.DoctorBooking.objects.create(clinic=cls.c1, doctor=cls.d1,
status=2, premium_booking=True,
patient_name="example",
patient_phone_number="+9747721234",
confirmed_date=datetime.strptime(
"16 Jun 2017 14:22:26:000 +0300",
receive_format),
handled_on=datetime.strptime(
"17 Jun 2017 14:22:26:000 +0300",
receive_format))
The second line from above would call it's save()
function that would call save()
on cls.d1
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
self.doctor.save() # <- here it raises an IntegrityError
This is the simplest code I could extract, but this happens all over for different classes.
To reiterate this gives me the following.
psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
DETAIL: Key (id)=(7) already exists.
I'm not even sure why this is happening. When you create an object shouldn't psycopg2
take care of auto-incrementing the pk
? From what I can gather the database doesn't have any issues, when I add a breakpoint before the .save()
and check the Database a doctor with the same data and pk
is already in the database. So I'm guessing it's assuming that these two objects are different... but I'm calling create then save NOT create twice.
EDIT: Solved in the comments :D
django postgresql django-2.1
add a comment |
I upgraded from Django 1.11 to Django 2.0 and my tests started failing. I have 7 TestCase
classes and all use the setUpTestData
provided by Django. When I run them all together one of them fails to set up because of an psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
.
When I run one of those TestCase
classes alone it works fine. It seems like they're influencing each other in some way, but it's strange that it would fail after upgrading to Django 2.0. I've also noticed it's not at the create()
it's at the save()
.
In the setup for the dashboards
app I have some creation data:
cls.d1 = doctor_models.Doctor.objects.create(email="johndoe@example.com", name="John Doe",
specialization=cls.s1, premium=True,
premium_price=4310, consultation_price=341)
...
cls.b1 = doctor_models.DoctorBooking.objects.create(clinic=cls.c1, doctor=cls.d1,
status=2, premium_booking=True,
patient_name="example",
patient_phone_number="+9747721234",
confirmed_date=datetime.strptime(
"16 Jun 2017 14:22:26:000 +0300",
receive_format),
handled_on=datetime.strptime(
"17 Jun 2017 14:22:26:000 +0300",
receive_format))
The second line from above would call it's save()
function that would call save()
on cls.d1
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
self.doctor.save() # <- here it raises an IntegrityError
This is the simplest code I could extract, but this happens all over for different classes.
To reiterate this gives me the following.
psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
DETAIL: Key (id)=(7) already exists.
I'm not even sure why this is happening. When you create an object shouldn't psycopg2
take care of auto-incrementing the pk
? From what I can gather the database doesn't have any issues, when I add a breakpoint before the .save()
and check the Database a doctor with the same data and pk
is already in the database. So I'm guessing it's assuming that these two objects are different... but I'm calling create then save NOT create twice.
EDIT: Solved in the comments :D
django postgresql django-2.1
Btw why are you trying to saved doctor after saving the model ?
– Mohammad Umair
Dec 31 '18 at 14:17
add a comment |
I upgraded from Django 1.11 to Django 2.0 and my tests started failing. I have 7 TestCase
classes and all use the setUpTestData
provided by Django. When I run them all together one of them fails to set up because of an psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
.
When I run one of those TestCase
classes alone it works fine. It seems like they're influencing each other in some way, but it's strange that it would fail after upgrading to Django 2.0. I've also noticed it's not at the create()
it's at the save()
.
In the setup for the dashboards
app I have some creation data:
cls.d1 = doctor_models.Doctor.objects.create(email="johndoe@example.com", name="John Doe",
specialization=cls.s1, premium=True,
premium_price=4310, consultation_price=341)
...
cls.b1 = doctor_models.DoctorBooking.objects.create(clinic=cls.c1, doctor=cls.d1,
status=2, premium_booking=True,
patient_name="example",
patient_phone_number="+9747721234",
confirmed_date=datetime.strptime(
"16 Jun 2017 14:22:26:000 +0300",
receive_format),
handled_on=datetime.strptime(
"17 Jun 2017 14:22:26:000 +0300",
receive_format))
The second line from above would call it's save()
function that would call save()
on cls.d1
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
self.doctor.save() # <- here it raises an IntegrityError
This is the simplest code I could extract, but this happens all over for different classes.
To reiterate this gives me the following.
psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
DETAIL: Key (id)=(7) already exists.
I'm not even sure why this is happening. When you create an object shouldn't psycopg2
take care of auto-incrementing the pk
? From what I can gather the database doesn't have any issues, when I add a breakpoint before the .save()
and check the Database a doctor with the same data and pk
is already in the database. So I'm guessing it's assuming that these two objects are different... but I'm calling create then save NOT create twice.
EDIT: Solved in the comments :D
django postgresql django-2.1
I upgraded from Django 1.11 to Django 2.0 and my tests started failing. I have 7 TestCase
classes and all use the setUpTestData
provided by Django. When I run them all together one of them fails to set up because of an psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
.
When I run one of those TestCase
classes alone it works fine. It seems like they're influencing each other in some way, but it's strange that it would fail after upgrading to Django 2.0. I've also noticed it's not at the create()
it's at the save()
.
In the setup for the dashboards
app I have some creation data:
cls.d1 = doctor_models.Doctor.objects.create(email="johndoe@example.com", name="John Doe",
specialization=cls.s1, premium=True,
premium_price=4310, consultation_price=341)
...
cls.b1 = doctor_models.DoctorBooking.objects.create(clinic=cls.c1, doctor=cls.d1,
status=2, premium_booking=True,
patient_name="example",
patient_phone_number="+9747721234",
confirmed_date=datetime.strptime(
"16 Jun 2017 14:22:26:000 +0300",
receive_format),
handled_on=datetime.strptime(
"17 Jun 2017 14:22:26:000 +0300",
receive_format))
The second line from above would call it's save()
function that would call save()
on cls.d1
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
self.doctor.save() # <- here it raises an IntegrityError
This is the simplest code I could extract, but this happens all over for different classes.
To reiterate this gives me the following.
psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
DETAIL: Key (id)=(7) already exists.
I'm not even sure why this is happening. When you create an object shouldn't psycopg2
take care of auto-incrementing the pk
? From what I can gather the database doesn't have any issues, when I add a breakpoint before the .save()
and check the Database a doctor with the same data and pk
is already in the database. So I'm guessing it's assuming that these two objects are different... but I'm calling create then save NOT create twice.
EDIT: Solved in the comments :D
django postgresql django-2.1
django postgresql django-2.1
edited Jan 2 at 8:16
Ysrninja
asked Dec 31 '18 at 14:05
YsrninjaYsrninja
1918
1918
Btw why are you trying to saved doctor after saving the model ?
– Mohammad Umair
Dec 31 '18 at 14:17
add a comment |
Btw why are you trying to saved doctor after saving the model ?
– Mohammad Umair
Dec 31 '18 at 14:17
Btw why are you trying to saved doctor after saving the model ?
– Mohammad Umair
Dec 31 '18 at 14:17
Btw why are you trying to saved doctor after saving the model ?
– Mohammad Umair
Dec 31 '18 at 14:17
add a comment |
1 Answer
1
active
oldest
votes
Most probably, the referred doctor
is already saved. Check for that before trying to re-save.
Something like this:
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
if not self.doctor.pk:
self.doctor.save()
Thanks for the reply! But why does this matter? I still need to save the doctor. In Django 1.11 this was working fine. Do you have any other ideas?
– Ysrninja
Dec 31 '18 at 14:30
1
Before you can add reference to any object (here, doctor) in another object (here, DoctorBooking) you must have that object (here, doctor) in database. So before you call save of DoctorBooking, doctor must already be in database, which you're already doing cls.d1= ... , so why are you again trying to save doctor ?
– Mohammad Umair
Dec 31 '18 at 14:32
Because the ranking of the doctor changes whenever a new booking happens, the same thing happens when I use signals. When I try to useupdate_fields
it breaks because it can't find it in the database.... so I don't know
– Ysrninja
Dec 31 '18 at 15:04
1
So I guess you need to update the rank of doctor, where are you doing that ?
– Mohammad Umair
Jan 1 at 18:06
I already figured out the problem, it's completely irrelevant to this. It was something else. Basically Django was trying to create a new object insave
because it couldn't find an object with a similar PK. This is because Doctor has it's own manager withget_queryset
that returns only a set of the doctors based on something, and it wasn't returning the doctors with a similar PK so it would clash. One last thing, I'll upvote you for helping me, can you please upvote me to get over the 15 rep limit.
– Ysrninja
Jan 2 at 8:14
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988348%2fintegrityerror-in-testcase-runs-after-updating-to-django-2-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Most probably, the referred doctor
is already saved. Check for that before trying to re-save.
Something like this:
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
if not self.doctor.pk:
self.doctor.save()
Thanks for the reply! But why does this matter? I still need to save the doctor. In Django 1.11 this was working fine. Do you have any other ideas?
– Ysrninja
Dec 31 '18 at 14:30
1
Before you can add reference to any object (here, doctor) in another object (here, DoctorBooking) you must have that object (here, doctor) in database. So before you call save of DoctorBooking, doctor must already be in database, which you're already doing cls.d1= ... , so why are you again trying to save doctor ?
– Mohammad Umair
Dec 31 '18 at 14:32
Because the ranking of the doctor changes whenever a new booking happens, the same thing happens when I use signals. When I try to useupdate_fields
it breaks because it can't find it in the database.... so I don't know
– Ysrninja
Dec 31 '18 at 15:04
1
So I guess you need to update the rank of doctor, where are you doing that ?
– Mohammad Umair
Jan 1 at 18:06
I already figured out the problem, it's completely irrelevant to this. It was something else. Basically Django was trying to create a new object insave
because it couldn't find an object with a similar PK. This is because Doctor has it's own manager withget_queryset
that returns only a set of the doctors based on something, and it wasn't returning the doctors with a similar PK so it would clash. One last thing, I'll upvote you for helping me, can you please upvote me to get over the 15 rep limit.
– Ysrninja
Jan 2 at 8:14
add a comment |
Most probably, the referred doctor
is already saved. Check for that before trying to re-save.
Something like this:
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
if not self.doctor.pk:
self.doctor.save()
Thanks for the reply! But why does this matter? I still need to save the doctor. In Django 1.11 this was working fine. Do you have any other ideas?
– Ysrninja
Dec 31 '18 at 14:30
1
Before you can add reference to any object (here, doctor) in another object (here, DoctorBooking) you must have that object (here, doctor) in database. So before you call save of DoctorBooking, doctor must already be in database, which you're already doing cls.d1= ... , so why are you again trying to save doctor ?
– Mohammad Umair
Dec 31 '18 at 14:32
Because the ranking of the doctor changes whenever a new booking happens, the same thing happens when I use signals. When I try to useupdate_fields
it breaks because it can't find it in the database.... so I don't know
– Ysrninja
Dec 31 '18 at 15:04
1
So I guess you need to update the rank of doctor, where are you doing that ?
– Mohammad Umair
Jan 1 at 18:06
I already figured out the problem, it's completely irrelevant to this. It was something else. Basically Django was trying to create a new object insave
because it couldn't find an object with a similar PK. This is because Doctor has it's own manager withget_queryset
that returns only a set of the doctors based on something, and it wasn't returning the doctors with a similar PK so it would clash. One last thing, I'll upvote you for helping me, can you please upvote me to get over the 15 rep limit.
– Ysrninja
Jan 2 at 8:14
add a comment |
Most probably, the referred doctor
is already saved. Check for that before trying to re-save.
Something like this:
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
if not self.doctor.pk:
self.doctor.save()
Most probably, the referred doctor
is already saved. Check for that before trying to re-save.
Something like this:
def save(self, *args, **kwargs):
if self.doctor.premium:
self.premium_booking = True
else:
self.premium_booking = False
super(DoctorBooking, self).save(*args, **kwargs)
if not self.doctor.pk:
self.doctor.save()
answered Dec 31 '18 at 14:15
Mohammad UmairMohammad Umair
2,1181720
2,1181720
Thanks for the reply! But why does this matter? I still need to save the doctor. In Django 1.11 this was working fine. Do you have any other ideas?
– Ysrninja
Dec 31 '18 at 14:30
1
Before you can add reference to any object (here, doctor) in another object (here, DoctorBooking) you must have that object (here, doctor) in database. So before you call save of DoctorBooking, doctor must already be in database, which you're already doing cls.d1= ... , so why are you again trying to save doctor ?
– Mohammad Umair
Dec 31 '18 at 14:32
Because the ranking of the doctor changes whenever a new booking happens, the same thing happens when I use signals. When I try to useupdate_fields
it breaks because it can't find it in the database.... so I don't know
– Ysrninja
Dec 31 '18 at 15:04
1
So I guess you need to update the rank of doctor, where are you doing that ?
– Mohammad Umair
Jan 1 at 18:06
I already figured out the problem, it's completely irrelevant to this. It was something else. Basically Django was trying to create a new object insave
because it couldn't find an object with a similar PK. This is because Doctor has it's own manager withget_queryset
that returns only a set of the doctors based on something, and it wasn't returning the doctors with a similar PK so it would clash. One last thing, I'll upvote you for helping me, can you please upvote me to get over the 15 rep limit.
– Ysrninja
Jan 2 at 8:14
add a comment |
Thanks for the reply! But why does this matter? I still need to save the doctor. In Django 1.11 this was working fine. Do you have any other ideas?
– Ysrninja
Dec 31 '18 at 14:30
1
Before you can add reference to any object (here, doctor) in another object (here, DoctorBooking) you must have that object (here, doctor) in database. So before you call save of DoctorBooking, doctor must already be in database, which you're already doing cls.d1= ... , so why are you again trying to save doctor ?
– Mohammad Umair
Dec 31 '18 at 14:32
Because the ranking of the doctor changes whenever a new booking happens, the same thing happens when I use signals. When I try to useupdate_fields
it breaks because it can't find it in the database.... so I don't know
– Ysrninja
Dec 31 '18 at 15:04
1
So I guess you need to update the rank of doctor, where are you doing that ?
– Mohammad Umair
Jan 1 at 18:06
I already figured out the problem, it's completely irrelevant to this. It was something else. Basically Django was trying to create a new object insave
because it couldn't find an object with a similar PK. This is because Doctor has it's own manager withget_queryset
that returns only a set of the doctors based on something, and it wasn't returning the doctors with a similar PK so it would clash. One last thing, I'll upvote you for helping me, can you please upvote me to get over the 15 rep limit.
– Ysrninja
Jan 2 at 8:14
Thanks for the reply! But why does this matter? I still need to save the doctor. In Django 1.11 this was working fine. Do you have any other ideas?
– Ysrninja
Dec 31 '18 at 14:30
Thanks for the reply! But why does this matter? I still need to save the doctor. In Django 1.11 this was working fine. Do you have any other ideas?
– Ysrninja
Dec 31 '18 at 14:30
1
1
Before you can add reference to any object (here, doctor) in another object (here, DoctorBooking) you must have that object (here, doctor) in database. So before you call save of DoctorBooking, doctor must already be in database, which you're already doing cls.d1= ... , so why are you again trying to save doctor ?
– Mohammad Umair
Dec 31 '18 at 14:32
Before you can add reference to any object (here, doctor) in another object (here, DoctorBooking) you must have that object (here, doctor) in database. So before you call save of DoctorBooking, doctor must already be in database, which you're already doing cls.d1= ... , so why are you again trying to save doctor ?
– Mohammad Umair
Dec 31 '18 at 14:32
Because the ranking of the doctor changes whenever a new booking happens, the same thing happens when I use signals. When I try to use
update_fields
it breaks because it can't find it in the database.... so I don't know– Ysrninja
Dec 31 '18 at 15:04
Because the ranking of the doctor changes whenever a new booking happens, the same thing happens when I use signals. When I try to use
update_fields
it breaks because it can't find it in the database.... so I don't know– Ysrninja
Dec 31 '18 at 15:04
1
1
So I guess you need to update the rank of doctor, where are you doing that ?
– Mohammad Umair
Jan 1 at 18:06
So I guess you need to update the rank of doctor, where are you doing that ?
– Mohammad Umair
Jan 1 at 18:06
I already figured out the problem, it's completely irrelevant to this. It was something else. Basically Django was trying to create a new object in
save
because it couldn't find an object with a similar PK. This is because Doctor has it's own manager with get_queryset
that returns only a set of the doctors based on something, and it wasn't returning the doctors with a similar PK so it would clash. One last thing, I'll upvote you for helping me, can you please upvote me to get over the 15 rep limit.– Ysrninja
Jan 2 at 8:14
I already figured out the problem, it's completely irrelevant to this. It was something else. Basically Django was trying to create a new object in
save
because it couldn't find an object with a similar PK. This is because Doctor has it's own manager with get_queryset
that returns only a set of the doctors based on something, and it wasn't returning the doctors with a similar PK so it would clash. One last thing, I'll upvote you for helping me, can you please upvote me to get over the 15 rep limit.– Ysrninja
Jan 2 at 8:14
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988348%2fintegrityerror-in-testcase-runs-after-updating-to-django-2-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Btw why are you trying to saved doctor after saving the model ?
– Mohammad Umair
Dec 31 '18 at 14:17