django-positions
django-positions copied to clipboard
django.db.utils.IntegrityError when migrating a Postgres database.
Trying to upgrade a small Postgres database with the added PositionField. Using Django 1.7 with its new built-in migrations.
django.db.utils.IntegrityError: check constraint "minisites_page_position_check" is violated by some row
I eventually ditched this app and used s plain IntegerField instead (sadly)
can confirm this...
Same here
facing same issue.
Appears to have something to do with PositionField.get_internal_type()
which reports to the database that it is a PositiveIntegerField
. i think this causes a constraint to be written such that the value cannot be less than zero, which is a problem since the migration is setting the default to -1.
@jpwatts what is the benefit of overriding get_internal_type()
?
It tells the database that it's a PositiveIntegerField
because all valid positions are >= 0. Negative positions are shorthand for positions relative to the end of a collection. You can use them in code, but they are supposed to be resolved to absolute positions prior to being sent to the database. It looks like that isn't happening, but I don't think the fix is to remove the database constraint—it's to make sure invalid positions don't get sent to the database in the first place.
Just tested it again, and it still happens. Just to clarify, I added a new field. Is it possible that the field's definition is missing a default value?
It seems that Django migrations is likely using the default value of -1
at the database level when creating the new column for existing records, which is invalid for a PositiveIntegerField
Using kwarg default=0
seems to cause the error to go away.
You can simply change each negative value from your Database manually or create a function for that.
(incase if you are trying to SmallIntegerField()
-> PositiveSmallIntegerField()
This works for me.