Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
Ran into a bit of a problem, i'm getting the above error message when i run '
python manage.py syncdb
' I'm working on a fairly old site. It' running django 1.2.6 with a postgres DB.
The run didn't have south installed and i managed to get that working. Ran
python manage.py schemamigration --initial contact_enquiries
which ran fine and asked me to migrate. I then ran
python manage.py migrate contact_enquiries
I then got the same error as above.
It doesn't complaing about any of the syntax in my models which is why i'm confused. Here are my models and hopefully that will shed some light.
from django.db import models
class DocumentUpload(models.Model):
name = models.CharField(max_length="200")
document_upload = models.FileField(upload_to="uploads/documents")
def __unicode__(self):
return "%s" % self.name
class DocumentRequest(models.Model):
name = models.CharField(max_length="200")
company = models.CharField(max_length="200")
job_title = models.CharField(max_length="200")
email = models.EmailField(max_length="200")
report = models.ManyToManyField(DocumentUpload)
def __unicode__(self):
return "%s" % self.name
If you need anymore information, please let me know.
Thanks!
Although I am not 100% certain this is the problem, there is a good chance your sequence is out of date.
Does executing this within Postgres solve the issue?
SELECT setval('django_content_type_id_seq', (SELECT MAX(id) FROM django_content_type));
–
–
This usually means that your primary key sequence has gone out of sync. This may have been caused by a bad migration etc..
To fix this;
1. Start the dbshell
python manage.py dbshell
2. Find the current highest value of the primary key
select max(id) from django_content_type;
3. Change the primary key sequence to now start at a value higher than the value found in step 2.
So assuming the value returned in step 2 was 290780 then alter sequence to start at a number greater than 290780
alter sequence django_content_type_id_seq restart with 295000;
This one means that you have out of date sequence within your database (improper previous migrations or jumps between different versions of code which leads to migrations being applied in chaotic order can cause this broken state). To quickly workaround this issue you can manually update values in your database.
python manage.py dbshell
check the current value of following table
SELECT last_value FROM django_migrations_id_seq;
SELECT last_value FROM django_content_type_id_seq;
Then update them with the command below (any sane values that are greater than ones from the output above). Usually, the first command is enough but if you are still getting errors with key value violates unique constraint "django_content_type_pkey" you need to run the second one as well (you might need to alter auth_permission_id_seq too depending on your database state)
ALTER SEQUENCE django_migrations_id_seq RESTART WITH {value_greater_than_last_value};
ALTER SEQUENCE django_content_type_id_seq RESTART WITH {value_greater_than_last_value};
Even after this, you are getting error. You use
SELECT last_value FROM auth_permission_id_seq;
Increment value by one
ALTER SEQUENCE auth_permission_id_seq RESTART WITH {value_greater_than_last_value};
I received this error:
django.db.utils.IntegrityError: duplicate key value violates unique constraint
"blahmodule_blahthing_blahstuff_id"
DETAIL: Key (blahstuff_id)=(1) already exists.
A possible solution:
Try migrating the blahstuff
relation in blahthing
from a OneToOneField
field to a ForeignKey
An explanation with what I was using:
I was using the Django RestFramework which was wired up through a ListCreateAPIView
, and a ModelSerializer
. For whatever reason the OneToOneField
attaches a UNIQUE
to one of the join tables blahmodule_blahthing_blahstuff_id
, ( i'm not actually sure if its a join table, this is an assumption ), but whatever magic is happening in django switching to a ForeignKey with QuerySet returns, solved my issue.
related:
What's the difference between django OneToOneField and ForeignKey?
https://www.postgresql.org/docs/11/ddl-constraints.html
As far as the actual question above:
schema :
-- Table: public.django_content_type
-- DROP TABLE public.django_content_type;
CREATE TABLE public.django_content_type
id integer NOT NULL DEFAULT nextval('django_content_type_id_seq'::regclass),
app_label character varying(100) COLLATE pg_catalog."default" NOT NULL,
model character varying(100) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT django_content_type_pkey PRIMARY KEY (id),
CONSTRAINT django_content_type_app_label_model_76bd3d3b_uniq UNIQUE (app_label, model)
WITH (
OIDS = FALSE
TABLESPACE pg_default;
ALTER TABLE public.django_content_type
OWNER to YOURUSER;
Example rows:
pk app_label model
1 "your_app" "your_app_model"
2 "your_app" "your_app_model"
3 "blah_module" "blahthing"
4 "blah_module" "blahstuff"
The hash on django_content_type_app_label_model_76bd3d3b_uniq
and the Primary Keys can get jumbled up if a migration fails mid process.
–
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.