doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.89k stars 355 forks source link

error on `unique=True` #454

Open mosi-kha opened 4 years ago

mosi-kha commented 4 years ago

use AbstractUser to extend user and add mobile field with unique=True , for test when add a duplicated mobile it's raise a No exception message supplied message and when use same username that raise correct message that username is exist.

django = 3.0.8 djongo = 1.3.3 pymongo = 3.10.1 and test on 3.11.0

user model

class User(AbstractUser):
    mobile = models.CharField(max_length=15,
                              unique=True,
                              # blank=False,
                              # null=False,
                              error_messages={
                                  'unique': "A user with that mobile already exists."
                              },
                              )

Traceback

File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 856, in parse return handler(self, statement) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 928, in _insert query.execute() File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 397, in execute res = self.db[self.left_table].insert_many(docs, ordered=False) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/collection.py", line 761, in insert_many blk.execute(write_concern, session=session) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/bulk.py", line 528, in execute return self.execute_command(generator, write_concern, session) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/bulk.py", line 363, in execute_command _raise_bulk_write_error(full_result) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/bulk.py", line 140, in _raise_bulk_write_error raise BulkWriteError(full_result) pymongo.errors.BulkWriteError: batch op errors occurred, full error: {'writeErrors': [{'index': 0, 'code': 11000, 'keyPattern': {'mobile': 1}, 'keyValue': {'mobile': '09358459555'}, 'errmsg': 'E11000 duplicate key error collection: spexa_core.accounts_user index: accounts_user_mobile_68a76a25_uniq dup key: { mobile: "09358459555" }', 'op': {'user_id': ObjectId('5f338f4b950b8e7aed6f8682'), 'password': '123456', 'last_login': None, 'is_superuser': False, 'username': 'testt', 'first_name': '', 'last_name': '', 'email': '', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2020, 8, 12, 6, 42, 19, 494489), 'mobile': '09358459555', 'is_2fa_active': True, 'has_my_storage': False, '_id': ObjectId('5f338f4b950b8e7aed6f8683')}}], 'writeConcernErrors': [], 'nInserted': 0, 'nUpserted': 0, 'nMatched': 0, 'nModified': 0, 'nRemoved': 0, 'upserted': []}

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute self.result = Query( File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 783, in init self._query = self.parse() File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 868, in parse raise exe from e djongo.exceptions.SQLDecodeError:

Keyword: None
Sub SQL: None
FAILED SQL: INSERT INTO "accounts_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined", "mobile", "user_id", "is_2fa_active", "has_my_storage") VALUES (%(0)s, %(1)s, %(2)s, %(3)s, %(4)s, %(5)s, %(6)s, %(7)s, %(8)s, %(9)s, %(10)s, %(11)s, %(12)s, %(13)s)
Params: ('123456', None, False, 'testt', '', '', '', False, True, datetime.datetime(2020, 8, 12, 6, 42, 19, 494489), '09358459555', ObjectId('5f338f4b950b8e7aed6f8682'), True, False)
Version: 1.3.3

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute raise db_exe from e djongo.database.DatabaseError

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, callback_kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, *kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch response = self.handle_exception(exc) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception self.raise_uncaught_exception(exc) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception raise exc File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch response = handler(request, *args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler return func(args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/spexa/apps/accounts/api/v1/user.py", line 33, in register user = serializer.save() File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 212, in save self.instance = self.create(validated_data) File "/home/mos/Project/Python/spexa/spexa-core/spexa/apps/accounts/serializers/user.py", line 25, in create user = super().create(validated_data) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 948, in create instance = ModelClass._default_manager.create(validated_data) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/query.py", line 433, in create obj.save(force_insert=True, using=self.db) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 66, in save super().save(*args, *kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save self.save_base(using=using, force_insert=force_insert, File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base updated = self._save_table( File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 887, in _save_table results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 924, in _do_insert return manager._insert( File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(args, **kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1392, in execute_sql cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute return super().execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/sentry_sdk/integrations/django/init.py", line 487, in execute return real_execute(self, sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute raise db_exe from e django.db.utils.DatabaseError Internal Server Error: /accounts/v1/users/register Traceback (most recent call last): File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 856, in parse return handler(self, statement) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 928, in _insert query.execute() File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 397, in execute res = self.db[self.left_table].insert_many(docs, ordered=False) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/collection.py", line 761, in insert_many blk.execute(write_concern, session=session) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/bulk.py", line 528, in execute return self.execute_command(generator, write_concern, session) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/bulk.py", line 363, in execute_command _raise_bulk_write_error(full_result) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/pymongo/bulk.py", line 140, in _raise_bulk_write_error raise BulkWriteError(full_result) pymongo.errors.BulkWriteError: batch op errors occurred, full error: {'writeErrors': [{'index': 0, 'code': 11000, 'keyPattern': {'mobile': 1}, 'keyValue': {'mobile': '09358459555'}, 'errmsg': 'E11000 duplicate key error collection: spexa_core.accounts_user index: accounts_user_mobile_68a76a25_uniq dup key: { mobile: "09358459555" }', 'op': {'user_id': ObjectId('5f338f4b950b8e7aed6f8682'), 'password': '123456', 'last_login': None, 'is_superuser': False, 'username': 'testt', 'first_name': '', 'last_name': '', 'email': '', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2020, 8, 12, 6, 42, 19, 494489), 'mobile': '09358459555', 'is_2fa_active': True, 'has_my_storage': False, '_id': ObjectId('5f338f4b950b8e7aed6f8683')}}], 'writeConcernErrors': [], 'nInserted': 0, 'nUpserted': 0, 'nMatched': 0, 'nModified': 0, 'nRemoved': 0, 'upserted': []}

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/cursor.py", line 51, in execute self.result = Query( File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 783, in init self._query = self.parse() File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/sql2mongo/query.py", line 868, in parse raise exe from e djongo.exceptions.SQLDecodeError:

Keyword: None
Sub SQL: None
FAILED SQL: INSERT INTO "accounts_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined", "mobile", "user_id", "is_2fa_active", "has_my_storage") VALUES (%(0)s, %(1)s, %(2)s, %(3)s, %(4)s, %(5)s, %(6)s, %(7)s, %(8)s, %(9)s, %(10)s, %(11)s, %(12)s, %(13)s)
Params: ('123456', None, False, 'testt', '', '', '', False, True, datetime.datetime(2020, 8, 12, 6, 42, 19, 494489), '09358459555', ObjectId('5f338f4b950b8e7aed6f8682'), True, False)
Version: 1.3.3

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute raise db_exe from e djongo.database.DatabaseError

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, callback_kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, *kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch response = self.handle_exception(exc) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception self.raise_uncaught_exception(exc) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception raise exc File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch response = handler(request, *args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler return func(args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/spexa/apps/accounts/api/v1/user.py", line 33, in register user = serializer.save() File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 212, in save self.instance = self.create(validated_data) File "/home/mos/Project/Python/spexa/spexa-core/spexa/apps/accounts/serializers/user.py", line 25, in create user = super().create(validated_data) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/rest_framework/serializers.py", line 948, in create instance = ModelClass._default_manager.create(validated_data) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(args, kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/query.py", line 433, in create obj.save(force_insert=True, using=self.db) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 66, in save super().save(*args, *kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save self.save_base(using=using, force_insert=force_insert, File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base updated = self._save_table( File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 887, in _save_table results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/base.py", line 924, in _do_insert return manager._insert( File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(args, **kwargs) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert return query.get_compiler(using=using).execute_sql(returning_fields) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1392, in execute_sql cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute return super().execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/sentry_sdk/integrations/django/init.py", line 487, in execute return real_execute(self, sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute return self.cursor.execute(sql, params) File "/home/mos/Project/Python/spexa/spexa-core/venv/lib/python3.8/site-packages/djongo/cursor.py", line 59, in execute raise db_exe from e django.db.utils.DatabaseError

mosi-kha commented 4 years ago

@nesdis

mosi-kha commented 4 years ago

pymongo error:

{'writeErrors': [{'index': 0, 'code': 11000, 'keyPattern': {'mobile': 1}, 'keyValue': {'mobile': '09358459555'}, 'errmsg': 'E11000 duplicate key error collection: spexa_core.accounts_user index: accounts_user_mobile_68a76a25_uniq dup key: { mobile: "09358459555" }', 'op': {'user_id': None, 'password': '123456', 'last_login': None, 'is_superuser': False, 'username': 'testt', 'first_name': '', 'last_name': '', 'email': '', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2020, 8, 12, 7, 14, 28, 617158), 'mobile': '09358459555', 'is_2fa_active': True, 'has_my_storage': False, '_id': ObjectId('5f3396d40028f425a60c92c9')}}], 'writeConcernErrors': [], 'nInserted': 0, 'nUpserted': 0, 'nMatched': 0, 'nModified': 0, 'nRemoved': 0, 'upserted': []}

need handler for duplicate key error