Transactions not working in Django using MySQL backend - mysql

I am regularily getting integrity errors for the following code snippet:
class StatsManager(Manager):
#transaction.atomic
def create(self, **kwargs):
kwargs.setdefault('date', date.today())
try:
obj = self.get_queryset().get(**kwargs)
except self.model.DoesNotExist:
obj = super().create(hits=1, **kwargs) # line 28
else:
obj.hits = F('hits') + 1
obj.save()
return obj
Here is the error message:
IntegrityError at /emploi/cours-particuliers-en-physique-chimie-en-classe-de-superieur-1-evry-14skb.html
(1062, "Duplicate entry 'EMP_VIEW-1903259-2018-01-08' for key 'statEvent'")
And the traceback:
File "/home/www/aladom_v6/www/stats/models/managers.py" in create
26. obj = self.get_queryset().get(**kwargs)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/query.py" in get
380. self.model._meta.object_name
During handling of the above exception (Stats matching query does not exist.), another exception occurred:
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django_mysql/monkey_patches.py" in execute
35. return orig_execute(self, sql, args)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
101. return self.cursor.execute(query, args)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
250. self.errorhandler(self, exc, value)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
50. raise errorvalue
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
247. res = self._query(query)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in _query
411. rowcount = self._do_query(q)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in _do_query
374. db.query(q)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/connections.py" in query
277. _mysql.connection.query(self, query)
The above exception ((1062, "Duplicate entry 'EMP_VIEW-1903259-2018-01-08' for key 'statEvent'")) was the direct cause of the following exception:
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/channels/handler.py" in process_exception_by_middleware
243. return super(AsgiHandler, self).process_exception_by_middleware(exception, request)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/home/www/aladom_v6/www/utils/views/behaviors.py" in dispatch
343. return super().dispatch(request, *args, **kwargs)
File "/home/www/aladom_v6/www/offers/views/public.py" in dispatch
1089. return super().dispatch(request, *args, **kwargs)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/home/www/aladom_v6/www/offers/views/public.py" in get
1308. return super().get(request, *args, **kwargs)
File "/home/www/aladom_v6/www/stats/views/behaviors.py" in get
16. target_id=self.object.pk)
File "/usr/lib/python3.4/contextlib.py" in inner
30. return func(*args, **kwds)
File "/home/www/aladom_v6/www/stats/models/managers.py" in create
28. obj = super().create(hits=1, **kwargs)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/query.py" in create
394. obj.save(force_insert=True, using=self.db)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/base.py" in save
808. force_update=force_update, update_fields=update_fields)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/base.py" in save_base
838. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/base.py" in _save_table
924. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/base.py" in _do_insert
963. using=using, raw=raw)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/query.py" in _insert
1076. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
1107. cursor.execute(sql, params)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/utils/six.py" in reraise
685. raise value.with_traceback(tb)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django_mysql/monkey_patches.py" in execute
35. return orig_execute(self, sql, args)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/django/db/backends/mysql/base.py" in execute
101. return self.cursor.execute(query, args)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
250. self.errorhandler(self, exc, value)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/connections.py" in defaulterrorhandler
50. raise errorvalue
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in execute
247. res = self._query(query)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in _query
411. rowcount = self._do_query(q)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/cursors.py" in _do_query
374. db.query(q)
File "/home/www/.virtualenvs/aladom/lib/python3.4/site-packages/MySQLdb/connections.py" in query
277. _mysql.connection.query(self, query)
Line 28 raises a duplicate entry error while I checked right above whether the key exists or not, and encapsulated it in a transaction.
I'm using Django 1.11, so I tried to set MySQL isolation level to read committed. I thought it would fix that issue, but it still occurs.
I also tried to do it the other way round:
class StatsManager(Manager):
def create(self, **kwargs):
kwargs.setdefault('date', date.today())
try:
obj = super().create(hits=1, **kwargs)
except IntegrityError:
obj = self.get_queryset().get(**kwargs) # line 27
obj.hits = F('hits') + 1
obj.save()
return obj
But in this case, it sometimes fail with the following error, because my requests are encapsulated in transactions:
TransactionManagementError at /admin/moderation/serviceoffermoderation/248424/change/
An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
Which is actually expected, unlike the former error.
Any idea what's going on and how to deal with it?

It looks like you could rewrite your code to use get_or_create, which has some handling for race conditions.
To avoid the An error occurred in the current transaction error, you need to wrap the code that can raise the IntegrityError with transaction.atomic:
def create(self, **kwargs):
kwargs.setdefault('date', date.today())
try:
with transaction.atomic():
obj = super().create(hits=1, **kwargs)
except IntegrityError:
obj = self.get_queryset().get(**kwargs) # line 27
obj.hits = F('hits') + 1
obj.save()
return obj
See the docs on controlling transactions explicitly for more info.

Related

sqlalchemy Object with OrderingList - Session.commit() method fails

Model that has attribute in type of sqlalchemy OrderingList fails on Database Session Commit step (as Database I am using PostgreSQL 13):
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship
from database.base_class import Base
from sqlalchemy.ext.orderinglist import ordering_list
class ContentType(Enum):
SCENE_HEADING = 'scene_heading'
ACTION = 'action'
CHARACTER = 'character'
PARENTHETICAL = 'parenthetical'
DIALOGUE = 'dialogue'
SHOT = 'shot'
TRANSITION = 'transition'
TEXT = 'text'
class EditorElement(Base):
"""
Class that represents the Table of Editor Elements.
"""
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
content = Column(String, nullable=True)
content_type = Column(ContentType, nullable=True)
screenplay_id = Column(Integer, ForeignKey('screenplays.id', ondelete="CASCADE"), nullable=False)
screenplay = relationship("Screenplay", back_populates="elements")
class Screenplay(Base):
"""
Class that represents the screenplay table in the database.
"""
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
name = Column(String, index=True, nullable=False)
elements = relationship("EditorElement", back_populates="screenplay", order_by="EditorElement.content_type",
collection_class=ordering_list('content_type'))
I am using FastAPI framework and in the CRUD file where the Screenplay is created, I have the following class:
class CRUDScreenplay(CRUDBase[models.Screenplay, schemas.ScreenplayBase, schemas.ScreenplayBase]):
#staticmethod
def create(db: Session, *, obj_in: schemas.ScreenplayCreate) -> models.Screenplay:
db_obj = models.Screenplay()
db_obj.name = obj_in['name']
for element in obj_in.get('elements'):
e = models.EditorElement(
content=element.content,
content_type=element.content_type,
screenplay_id=db_obj.id
)
db_obj.elements.append(e)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
Using the PyCharm debugger I was able to check when the error occurs, it seems to appear after Session.commit(). Here is the object elements before the commit() method
And here is the elements object after the commit() method.
And the error in the console:
INFO: 127.0.0.1:52649 - "POST /api/v1/screenplays/ HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\sql\sqltypes.py", line 1649, in _object_value_for_elem
return self._object_lookup[elem]
KeyError: 'scene_heading'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 372, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\fastapi\applications.py", line 261, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\middleware\cors.py", line 84, in __call__
await self.app(scope, receive, send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
raise e
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\routing.py", line 259, in handle
await self.app(scope, receive, send)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\routing.py", line 61, in app
response = await func(request)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\fastapi\routing.py", line 235, in app
response_data = await serialize_response(
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\fastapi\routing.py", line 130, in serialize_response
value, errors_ = await run_in_threadpool(
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\starlette\concurrency.py", line 39, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\anyio\to_thread.py", line 28, in run_sync
return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\anyio\_backends\_asyncio.py", line 818, in run_sync_in_worker_thread
return await future
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\anyio\_backends\_asyncio.py", line 754, in run
result = context.run(func, *args)
File "pydantic\fields.py", line 854, in pydantic.fields.ModelField.validate
File "pydantic\fields.py", line 1071, in pydantic.fields.ModelField._validate_singleton
File "pydantic\fields.py", line 1118, in pydantic.fields.ModelField._apply_validators
File "pydantic\class_validators.py", line 313, in pydantic.class_validators._generic_validator_basic.lambda12
File "pydantic\main.py", line 678, in pydantic.main.BaseModel.validate
File "pydantic\main.py", line 562, in pydantic.main.BaseModel.from_orm
File "pydantic\main.py", line 1001, in pydantic.main.validate_model
File "pydantic\utils.py", line 409, in pydantic.utils.GetterDict.get
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\orm\attributes.py", line 481, in __get__
return self.impl.get(state, dict_)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\orm\attributes.py", line 941, in get
value = self._fire_loader_callables(state, key, passive)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\orm\attributes.py", line 977, in _fire_loader_callables
return self.callable_(state, passive)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\orm\strategies.py", line 911, in _load_for_state
return self._emit_lazyload(
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\orm\strategies.py", line 1051, in _emit_lazyload
result = result.unique().scalars().all()
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\engine\result.py", line 1371, in all
return self._allrows()
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\engine\result.py", line 401, in _allrows
rows = self._fetchall_impl()
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\engine\result.py", line 1284, in _fetchall_impl
return self._real_result._fetchall_impl()
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\engine\result.py", line 1696, in _fetchall_impl
return list(self.iterator)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\orm\loading.py", line 147, in chunks
fetch = cursor._raw_all_rows()
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\engine\result.py", line 393, in _raw_all_rows
return [make_row(row) for row in rows]
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\engine\result.py", line 393, in <listcomp>
return [make_row(row) for row in rows]
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\sql\sqltypes.py", line 1768, in process
value = self._object_value_for_elem(value)
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\sql\sqltypes.py", line 1651, in _object_value_for_elem
util.raise_(
File "C:\Users\userh\Playground\screenplay-writer\backend\.venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
LookupError: 'scene_heading' is not among the defined enum values. Enum name: None. Possible values: None
However, the data was successfully created in the database, but FastAPI does not return anything due to this error.
There are some related issues in GitHub but I am was not able to figure out what is going on there:
Issue 1
Issue 2
Issue 3
Issue 4
UPDATE
Alembic (which is often used with FastAPI) or SLQAlchemy created the PostgreSQL ENUM Type in Uppercase.
I changed the definition of ContentType(enum.Enum) as it was in the example of the official documentation LINK. Thanks to #fchancel for pointing on that:
class ContentType(str, enum.Enum):
HEADING = 'HEADING'
ACTION = 'ACTION'
CHARACTER = 'CHARACTER'
PARENTHETICAL = 'PARENTHETICAL'
DIALOGUE = 'DIALOGUE'
SHOT = 'SHOT'
TRANSITION = 'TRANSITION'
TEXT = 'TEXT'
So, now my code works
It seems that when creating your elements, the content_type section is a problem, since it can't find heading_scene. To understand this, we need to see how you call your function and what obj_in contains.
However, it's possible that the source of the error is just your class ContentType(Enum) which to work properly should be class ContentType(str, Enum)

(1054, "Unknown column 'leçon_lesson.subject_id' in 'field list'")

i have been making some changes on my model 'lesson' and suddenly i couldn't use my model on my django website with MySql data base.
when i try to use it on a view i got this error
(1054, "Unknown column 'leçon_lesson.subject_id' in 'field list'")
the commands makemigrations and migrate works fine but this error occurs when using the model only
this is the model.py
from django.db import models
from .validators import *
from scolarité.models.level import Level
from scolarité.models.subject import Subject
class Lesson(models.Model):
level = models.ForeignKey(Level,on_delete=models.CASCADE)
subject = models.ForeignKey(Subject,on_delete=models.CASCADE)
chapiter = models.CharField(max_length=200)
lesson = models.CharField(max_length=200)
skill = models.CharField(max_length=200)
vacations = models.IntegerField()
link = models.URLField(max_length=700,null=True,blank=True)
remarques = models.TextField(null=True,blank=True)
order = models.IntegerField()
created = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now=True)
state = models.BooleanField(default=False)
def __str__(self):
return self.lesson
views.py
#=========================== view lessons =====================
#login_required #use this to make the view accessible for logged in users only
def view_lessons_list(request,subject_id):
request.session['subject_id']= subject_id #assign subject id value to session
level = Level.objects.get(id=request.session['level_id']) #getting the level model
subject = Subject.objects.get(id=request.session['subject_id']) #getting the subject model
lessons = Lesson.objects.filter(subject=subject ,level=level) #filtering the lesson based on the chosen level and subject
context={'lessons':lessons,}
return render(request,'leçon/view_lessons_list.html',context)
the traceback
Traceback (most recent call last):
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\leçon\views.py", line 25, in view_lessons_list
return render(request,'leçon/view_lessons_list.html',context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\loader.py", line 68, in render_to_string
return template.render(context, request)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 207, in render
return self._render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\loader_tags.py", line 72, in render
result = block.nodelist.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\template\defaulttags.py", line 173, in render
len_values = len(values)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\models\query.py", line 232, in __len__
self._fetch_all()
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\models\query.py", line 1102, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\models\query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\models\sql\compiler.py", line 876, in execute_sql
cursor.execute(sql, params)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\backends\utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\backends\utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
return self.cursor.execute(query, args)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
res = self._query(query)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\YAHYA-PC\Desktop\CourseCode\env\lib\site-packages\MySQLdb\connections.py", line 292, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1054, "Unknown column 'leçon_lesson.subject_id' in 'field list'")
[11/Oct/2020 20:36:35] "GET /le%C3%A7on/view_lessons_list/18/ HTTP/1.1" 500 299785
i have been trying deleting the migration files and and different youtube tutorials but nothing seems to work for me i keep getting different database errors and start all over again with this error
is there any solution or fix for this matter ?it's really frustrating and letting me down
i just found out a solution not the most efficient but it works i just created a new app with different name and moved all the models and views to it and run makemigrations and migrate and that's it

How to solve django.db.utils.DataError: (1406, "Data too long for column 'product_team' at row 1")

After running a function containing the below create process :
client_daily.objects.create(client_id=c,title=c.title,products=product_list,categories=categories[:-2],product_team=product_team[:-2],pcategory=pcategory[:-2],date=date)
I came up with the error:
Traceback:
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py" in execute
112. return self.cursor.execute(query, args)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/MySQLdb/cursors.py" in execute
209. res = self._query(query)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/MySQLdb/cursors.py" in _query
315. db.query(q)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/MySQLdb/connections.py" in query
239. _mysql.connection.query(self, query)
The above exception ((1406, "Data too long for column 'product_team' at row 1")) was the direct cause of the following exception:
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/www/wwwroot/geolocator/intranet/views.py" in cron_job_functions
2103. message=my_scheduled_job()
File "/www/wwwroot/geolocator/intranet/views.py" in my_scheduled_job
882. client_daily.objects.create(client_id=c,title=c.title,products=product_list,categories=categories[:-2],product_team=product_team[:-2],pcategory=pcategory[:-2],date=date)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/query.py" in create
399. obj.save(force_insert=True, using=self.db)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/base.py" in save
796. force_update=force_update, update_fields=update_fields)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/base.py" in save_base
824. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/base.py" in _save_table
908. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/base.py" in _do_insert
947. using=using, raw=raw)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/query.py" in _insert
1045. return query.get_compiler(using=using).execute_sql(return_id)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
1054. cursor.execute(sql, params)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/utils/six.py" in reraise
685. raise value.with_traceback(tb)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/django/db/backends/mysql/base.py" in execute
112. return self.cursor.execute(query, args)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/MySQLdb/cursors.py" in execute
209. res = self._query(query)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/MySQLdb/cursors.py" in _query
315. db.query(q)
File "/www/wwwroot/geolocator/geolocator_venv/lib/python3.7/site-packages/MySQLdb/connections.py" in query
239. _mysql.connection.query(self, query)
Exception Type: DataError at /cron_job_functions/
Exception Value: (1406, "Data too long for column 'product_team' at row 1")
Here is my model :
class client_daily(models.Model):
client_id = models.ForeignKey(client_coordinates, null=True, blank=True,related_name='customers', verbose_name=u'πελάτες',on_delete = models.CASCADE)
title = models.CharField(max_length=200, blank=True, null=True)
products = models.CharField(max_length=4000, blank=True, null=True)
categories = models.CharField(max_length=200, blank=True, null=True,db_index=True)
product_team = models.CharField(max_length=350, blank=True, null=True,db_index=True)
pcategory = models.CharField(max_length=200, blank=True, null=True,db_index=True)
date = models.DateField(db_index=True)
I try to fix the issue by doing python manage.py migrate --fake but it keeps complaining.
How can i fix this?
Actually, CharField supports a max_length of 256 chars only for most database backends (You can check your database backend to find that value) . The value you are trying to pass in product_team was more than 256 chars. That's why you are getting the error. Change your product_team to TextField which does not any such limit.
You will face the same problem with your products field as well. So I would suggest you change that as well

I'm getting django.db.utils.IntegrityError: FOREIGN KEY constraint failed in cmd

I copied a json file from online. I'm using it for django project.
I entered the following lines in cmd -
>>> import json
>>> from blog.models import Post
>>> with open('posts.json') as f:
... posts_json=json.load(f)
...
>>> for post in posts_json:
... post = Post(title=post['title'], content=post['content'], author_id=post['
user_id'])
... post.save()
...
And the error I got was -
Traceback (most recent call last):
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: FOREIGN KEY constraint failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 3, in <module>
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 782, in save_base
updated = self._save_table(
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields
, raw)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\base.py", line 924, in _do_insert
return manager._insert(
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\models\sql\compiler.py", line 1391, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._e
xecute)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Admin\.virtualenvs\my_projects-I2onQxk3\lib\site-packages\djang
o\db\backends\sqlite3\base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: FOREIGN KEY constraint failed
This is my models.py file -
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title=models.CharField(max_length=100)
content=models.TextField()
date_posted=models.DateTimeField(default=timezone.now)
author=models.ForeignKey(User,on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail',kwargs={'pk':self.pk})
This is the json file I copied from online https://raw.githubusercontent.com/CoreyMSchafer/code_snippets/master/Django_Blog/snippets/posts.json
This is a later reply but maybe it will help someone else in the future.
I had the same issue now and the same code and posts.json as well, it seems that in posts.json the user_id it either 1 or 2 so the error means that there is no user with id 1 or 2 in your database.
I would say that there is no user with id = 2 I supposed you have created a superuser which is id = 1 and if you create a second user it may not take id = 2, so you need to find out what is that id and change it in your posts.json file.
That's how I just fixed my problem.
Sorry for my English.
The only foreign key here is author, so a foreign key constraint failing must mean that there's no user with an id corresponding to the user_id field in a record in that data.
Since by a quick glance it looks like there's only user ids 1 and 2, make sure you have those two users in your database, then try again.

Airflow DAG throws RecursionError triggered via Web console

I get following error, when I am trying to trigger DAG which is using custom BaseOperator.
here is the error,
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.7/site-packages/flask_admin/base.py", line 69, in inner
return self._run_view(f, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask_admin/base.py", line 368, in _run_view
return fn(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/flask_login/utils.py", line 258, in decorated_view
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/www/utils.py", line 290, in wrapper
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/www/utils.py", line 337, in wrapper
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/utils/db.py", line 74, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/www/views.py", line 1213, in trigger
external_trigger=True
File "/usr/local/lib/python3.7/site-packages/airflow/utils/db.py", line 74, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/models/dag.py", line 1659, in create_dagrun
session=session)
File "/usr/local/lib/python3.7/site-packages/airflow/utils/db.py", line 70, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/models/dag.py", line 1346, in create_dagrun
run.refresh_from_db()
File "/usr/local/lib/python3.7/site-packages/airflow/utils/db.py", line 74, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/airflow/models/dagrun.py", line 109, in refresh_from_db
DR.run_id == self.run_id
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3347, in one
ret = self.one_or_none()
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3316, in one_or_none
ret = list(self)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3389, in __iter__
return self._execute_and_instances(context)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3414, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1101, in _execute_clauseelement
distilled_params,
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1250, in _execute_context
e, statement, parameters, cursor, context
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1478, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
cursor, statement, parameters, context
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 588, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 272, in execute
self._handle_result(result)
File "/usr/local/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 163, in _handle_result
self._handle_resultset()
File "/usr/local/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 651, in _handle_resultset
self._rows = self._cnx.get_rows()[0]
File "/usr/local/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 318, in get_rows
else self._cmysql.fetch_row()
SystemError: <method 'fetch_row' of '_mysql_connector.MySQL' objects> returned a result with an error set
My Code is as follows:
from airflow.plugins_manager import AirflowPlugin
from airflow.utils.decorators import apply_defaults
class TestOperator(BaseOperator):
template_fields = ('param1')
ui_color = '#A7E6A7'
#apply_defaults
def __init__(self,param1,*args, **kwargs):
self.param1 = param1
super(TestOperator, self).__init__(*args, **kwargs)
def execute(self):
print ('welcome to airflow')
class TestOperatorPlugin(AirflowPlugin):
name = "TestOperator_plugin"
operators = [TestOperator]
--here is Dag,
from TestOperator import TestOperator
from airflow import DAG
from datetime import datetime
prog_args = {
'depends_on_past': False, 'param1' : 'testOne'
}
testMYDAG = DAG('TestMYDAG', start_date = datetime(2020, 2, 18) , description='TestMYDAG', default_args = prog_args, schedule_interval=None)
testOp = TestOperator(task_id='test_dag', dag=testMYDAG )
testOp
Team, I had resolved issue by adding encoding scheme to utf-8 in airflow.cfg file, Also, it needs to be appended to sql_alchemy_connection string.