doableware/djongo

django.db.utils.DatabaseError from admin site

tvandraren opened this issue · 0 comments

One line description of the issue

it all starts with a djongo.exceptions.SQLDecodeError when trying to create a model from the admin site. This isssue has never arised from my own code.

Python script

Not really sure what I should put there apart from the model:

from django.db import models
from django.utils import timezone

class Lengua(models.Model):
    nombre = models.TextField()
    lang_code = models.TextField()

class Palabra(models.Model):
    lengua = models.ForeignKey(Lengua, on_delete=models.PROTECT)
    lema = models.TextField()
    esActiva = models.BooleanField()
    fecha = models.DateTimeField(default=timezone.now)

Traceback

Internal Server Error: /admin/gramnerk/palabra/add/
Traceback (most recent call last):
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\djongo\sql2mongo\query.py", line 857, in parse
    return handler(self, statement)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\djongo\sql2mongo\query.py", line 929, in _insert
    query.execute()
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\djongo\sql2mongo\query.py", line 397, in execute
    res = self.db[self.left_table].insert_many(docs, ordered=False)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\pymongo\collection.py", line 770, in insert_many
    blk.execute(write_concern, session=session)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\pymongo\bulk.py", line 533, in execute
    return self.execute_command(generator, write_concern, session)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\pymongo\bulk.py", line 366, in execute_command
    _raise_bulk_write_error(full_result)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\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': {'id': 1}, 'keyValue': {'id': 1}, 'errmsg': 'E11000 duplicate key error collection: khunlyngramnerk_dev.gramnerk_palabra index: __primary_key__ dup key: { id: 1 }', 'op': {'id': 1, 'lengua_id': 11, 'lema': 'Belegaer', 'esActiva': False, 'fecha': datetime.datetime(2022, 5, 12, 11, 25, 36), '_id': ObjectId('627ceecbd5c3f6823652cc2a')}}], '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 "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\djongo\cursor.py", line 51, in execute
    self.result = Query(
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\djongo\sql2mongo\query.py", line 784, in __init__
    self._query = self.parse()
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\djongo\sql2mongo\query.py", line 869, in parse
    raise exe from e
djongo.exceptions.SQLDecodeError: 

	Keyword: None
	Sub SQL: None
	FAILED SQL: INSERT INTO "gramnerk_palabra" ("lengua_id", "lema", "esActiva", "fecha") VALUES (%(0)s, %(1)s, %(2)s, %(3)s)
	Params: [11, 'Belegaer', False, datetime.datetime(2022, 5, 12, 11, 25, 36)]
	Version: 1.3.6

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

Traceback (most recent call last):
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\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 "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\contrib\admin\options.py", line 683, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\utils\decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\views\decorators\cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\contrib\admin\sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\contrib\admin\options.py", line 1885, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\utils\decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\contrib\admin\options.py", line 1745, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\contrib\admin\options.py", line 1797, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\contrib\admin\options.py", line 1220, in save_model
    obj.save()
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\models\base.py", line 806, in save
    self.save_base(
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\models\base.py", line 857, in save_base
    updated = self._save_table(
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\models\base.py", line 1000, in _save_table
    results = self._do_insert(
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\models\base.py", line 1041, in _do_insert
    return manager._insert(
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\models\query.py", line 1434, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1621, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\backends\utils.py", line 103, in execute
    return super().execute(sql, params)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tvand\Documents\repos\khunlyngramnerk\venv\lib\site-packages\djongo\cursor.py", line 59, in execute
    raise db_exe from e
django.db.utils.DatabaseError
[12/May/2022 13:26:04] "POST /admin/gramnerk/palabra/add/ HTTP/1.1" 500 245178