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

Locality Integrity Error: django.db.utils.IntegrityError: (1062, "Duplicate entry... #53

Closed
keubs opened this issue Jun 27, 2016 · 3 comments
Assignees

Comments

@keubs
Copy link

keubs commented Jun 27, 2016

Hi,
I'm using MySQL for my database and noticed trying to INSERT new Localities into the DB returns an IntegrityError when the name+postal_code composite key is duplicated. Seeing that there are multiple zip codes that can exist under a single locality ("Orlando, FL, 32801", "Orlando, FL, 32802", etc...), wouldn't it make sense to build the composite key based around name+postal_code instead? Therefore you could have multiple zips under one Locality e.g. (Orlando-32801, Orlando-32802, etc...)?

See shell output below:

from address.models import Locality
l = Locality(name="Orlando", postal_code="32801", state_id=10)
l.save()
Traceback (most recent call last):
File "", line 1, in
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/models/base.py", line 710, in save
force_update=force_update, update_fields=update_fields)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/models/base.py", line 738, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/models/base.py", line 822, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/models/base.py", line 861, in _do_insert
using=using, raw=raw)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(_args, *_kwargs)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/models/query.py", line 920, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 974, in execute_sql
cursor.execute(sql, params)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/utils.py", line 97, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
raise value.with_traceback(tb)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/cursors.py", line 161, in execute
result = self._query(query)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/cursors.py", line 317, in _query
conn.query(q)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/connections.py", line 835, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/connections.py", line 1019, in _read_query_result
result.read()
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/connections.py", line 1302, in read
first_packet = self.connection._read_packet()
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/connections.py", line 981, in _read_packet
packet.check_error()
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/err.py", line 120, in raise_mysql_exception
_check_mysql_exception(errinfo)
File "/Users/kevincook/.venv/untitled/lib/python3.4/site-packages/pymysql/err.py", line 112, in _check_mysql_exception
raise errorclass(errno, errorvalue)
django.db.utils.IntegrityError: (1062, "Duplicate entry 'Orlando-10' for key 'address_locality_name_294493a5b63f9602_uniq'")

@keubs
Copy link
Author

keubs commented Jul 14, 2016

Adding my own comment here since the owner doesn't appear excited to answer. I find it's best to just drop that constraint altogether. You'll have plenty of instances in which the postal_code will differ but the state_code and name in the address_locality table will remain the same. You can fix this with the following steps:

SHOW CREATE TABLE address_locality;

find the index that binds "name" and "state_id", and drop the constraint with that name. In my case, address_locality_name_5eb72081b80e9d3d_uniq.

ALTER TABLE address_locality DROP INDEX address_locality_name_5eb72081b80e9d3d_uniq

@keubs
Copy link
Author

keubs commented Jul 20, 2016

A little more on this - While the steps above go so far as to remove the database constraint, there is still a built-in constraint living in the model declaration of the app. To remove this, you'll need to open up the models.py file in the app and comment out/delete the line from within the Locality(models.model) declaration that enforces this constraint. On my file, it's line 199: unique_together = ('name', 'state'). Hope this helps!

@banagale banagale self-assigned this Apr 10, 2020
@banagale banagale added this to the Ticket and PR Triage 1 milestone Apr 10, 2020
@banagale
Copy link
Collaborator

Thank you @keubs for this report. This was resolved as described in #63. Please see #98 for updates on django-address.

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

2 participants