Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django.db.utils.IntegrityError when migrating a Postgres database. #36

Open
danielbraun opened this issue Oct 7, 2014 · 9 comments
Open

Comments

@danielbraun
Copy link

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)

@bashu
Copy link

bashu commented Oct 17, 2014

can confirm this...

@ghost
Copy link

ghost commented Feb 11, 2015

Same here

@navyad
Copy link

navyad commented May 23, 2015

facing same issue.

@smcoll
Copy link
Contributor

smcoll commented Jun 11, 2015

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()?

@jpwatts
Copy link
Owner

jpwatts commented Jun 12, 2015

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.

@danielbraun
Copy link
Author

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?

@smcoll
Copy link
Contributor

smcoll commented Jun 24, 2015

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

@Solanar
Copy link

Solanar commented Nov 26, 2015

Using kwarg default=0 seems to cause the error to go away.

@deepanshu-nickelfox
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants