Related
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)
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
I have a model:
class Tour(models.Model):
INACTIVE = 0
ACTIVE = 1
ARCHIVE = 2
STATUS = (
(INACTIVE, "Inactive"),
(ACTIVE, "Active"),
(ARCHIVE, "Archive"),
)
AIR_TOUR = 0
GROUND_TOUR = 1
SEA_TOUR = 2
T_TYPES = (
(AIR_TOUR, "Air_tour"),
(GROUND_TOUR, "Ground_tour"),
(SEA_TOUR, "Sea_tour"),
)
title = models.CharField(max_length=200)
tour_from = models.ForeignKey(Airport, related_name='from_location')
tour_to = models.ForeignKey(Airport, related_name='to_location')
duration_day = models.IntegerField(default=1, blank=True, null=True)
duration_night = models.IntegerField(default=1, blank=True, null=True)
transport_type = models.IntegerField(default=AIR_TOUR, choices=T_TYPES, blank=True, null=True)
documents = models.TextField(default='', blank=True, null=True)
description = models.TextField(blank=True, null=True, default='')
services = models.TextField(blank=True, null=True, default='')
airline = models.ForeignKey(Airline, null=True, blank=True)
train = models.ForeignKey(Train, null=True, blank=True)
bus = models.ForeignKey(Bus, null=True, blank=True)
user = models.ForeignKey(User, related_name='user')
creator = models.ForeignKey(User, related_name='creator')
status = models.IntegerField(default=INACTIVE, choices=STATUS, blank=True, null=True)
create_at = models.DateTimeField(default='2000-10-10')
update_at = models.DateTimeField(default='2000-10-10')
I try to change status to ARCHIVE by this code:
test = Tour.objects.get(id=self.kwargs['pk'])
test.status = ARCHIVE
test.save()
and get this error:
The database backend does not accept 0 as a value for AutoField.
status field is not AutoField. Why this error happend?
UPDATE:
full stack trace:
Django version 1.11, using settings 'project.settings_local'
Starting development server at http://0.0.0.0:8888/
Quit the server with CONTROL-C.
Internal Server Error: /dashboard/tour/delete/1/
Traceback (most recent call last):
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/py/project/dashboard/views_tour.py", line 196, in post
tour.save()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 903, in _save_table
forced_update)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 953, in _do_update
return filtered._update(values) > 0
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/query.py", line 661, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1191, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 863, in execute_sql
sql, params = self.as_sql()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1157, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/related.py", line 963, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 955, in get_db_prep_value
value = connection.ops.validate_autopk_value(value)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 155, in validate_autopk_value
raise ValueError('The database backend does not accept 0 as a '
ValueError: The database backend does not accept 0 as a value for AutoField.
Internal Server Error: /dashboard/tour/delete/1/
Traceback (most recent call last):
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/py/project/dashboard/views_tour.py", line 196, in post
tour.save()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 903, in _save_table
forced_update)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/base.py", line 953, in _do_update
return filtered._update(values) > 0
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/query.py", line 661, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1191, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 863, in execute_sql
sql, params = self.as_sql()
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1157, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/related.py", line 963, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 766, in get_db_prep_save
prepared=False)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 955, in get_db_prep_value
value = connection.ops.validate_autopk_value(value)
File "/home/.virtualenvs/project/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 155, in validate_autopk_value
raise ValueError('The database backend does not accept 0 as a '
ValueError: The database backend does not accept 0 as a value for AutoField.
You likely have a foreign key field that contains a 0 (instead of null, or a valid value). If you don't have the proper foreign key constraints at the DB level (or you turned off foreign key checks when you added the constraints), there is nothing preventing invalid foreign key references from existing in that row.
Check the data in the all your foreign key fields for a 0: tour_from, tour_to,
airline, train, bus, user, creator.
What you're doing should work. Since the object is already in the database, it's strange that updating it with the same fields leads to a 0 somewhere (probably one of the foreign keys).
You should debug this and there are two ways to go at it:
Inspect your database using raw SQL. manage.py dbshell opens a SQL shell where you can do SELECT * FROM my_app_tour WHERE id = 1. Look at the particular row, maybe you can spot the issue.
Try test.save(update_fields=['status']). If that works, add more fields to update one by one, e.g. test.save(update_fields=['status', 'tour_from'], then test.save(update_fields['status', 'tour_from', 'tour_to'])... until the error is thrown again.
I suspect a database corruption, but there might be some other more serious issue in the way you create the objects. That would cause problems down the line.
Check your django_content_type table, look for 0's in the id column. Django logs database operations after each call, and it writes the ID of the content type.
I found that I had several entries with a 0 id. Updating these with unique numbers after the end of the sequence and restarting Django fixed this for me.
Honestly, I have no idea how it got that way. I'm using Django 2.2.
Here is the error log i get this error when i try to create a new user when i do:
u = User (...)
Error log:
Traceback (most recent call last)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/quechon/PycharmProjects/untitled3/myapp/login/routes.py", line 66, in _register_process
timecreated=datetime.datetime.utcnow()
File "<string>", line 4, in __init__
File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/state.py", line 414, in _initialize_instance
manager.dispatch.init_failure(self, args, kwargs)
File "/usr/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value
File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/state.py", line 411, in _initialize_instance
return manager.original_init(*mixed[1:], **kwargs)
File "/home/quechon/PycharmProjects/untitled3/myapp/models.py", line 80, in __init__
self.follow(self)
File "/home/quechon/PycharmProjects/untitled3/myapp/models.py", line 47, in follow
if not self.is_following(user):
File "/home/quechon/PycharmProjects/untitled3/myapp/models.py", line 57, in is_following
return self.followed.filter_by(followed_id=user.id).first() is not None
File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 1517, in filter_by
return self.filter(sql.and_(*clauses))
File "<string>", line 2, in filter
File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/base.py", line 198, in generate
self = args[0]._clone()
File "/usr/lib/python3.6/site-packages/sqlalchemy/orm/dynamic.py", line 278, in _clone
orm_util.instance_str(instance), self.attr.key))
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <User at 0x7fe2ac0e6e80> is not bound to a Session, and no contextual session is established; lazy load operation of attribute 'followed' cannot proceed
This is the model:
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True)
email = db.Column(db.String(100))
password = db.Column(db.Binary)
profilepic = db.Column(db.String)
timecreated = db.Column(db.DateTime)
#picture = db.relationship('Picture', backref='owner', lazy='dynamic')
comment = db.relationship('Comment', backref='user', lazy='dynamic')
post = db.relationship('Post', backref='user', lazy='dynamic')
followed = db.relationship('Follow', foreign_keys=[Follow.follower_id],
backref=db.backref('follower', lazy='joined'),
lazy='dynamic',
cascade='all, delete-orphan')
followers = db.relationship('Follow',
foreign_keys=[Follow.followed_id],
backref=db.backref('followed', lazy='joined'),
lazy='dynamic',
cascade='all, delete-orphan')
def follow(self, user):
if not self.is_following(user):
f = Follow(follower=self, followed=user)
db.session.add(f)
def unfollow(self, user):
f = self.followed.filter_by(followed_id=user.id).first()
if f:
db.session.delete(f)
def is_following(self, user):
return self.followed.filter_by(followed_id=user.id).first() is not None
def is_followed_by(self, user):
return self.followers.filter_by(follower_id=user.id).first() is not None
#property
def followed_posts(self):
return Post.query.join(Follow.followed_id == Post.user_id)\
.filter(Follow.follower_id == self.id)
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return str(self.id)
def __init__(self, **kwargs):
self.follow(self)
def __repr__(self):
return f'<id - {self.id} | - {self.username}>'
I would like to know why that happened and how to avoid this error next time
The only thing i found online about was to use lazy='subquery' instead of lazy='dynamic' but using that wont allow my query to use filter_by attribute.
It looks like you are trying to interact with your flask app via the interactive shell. You can do this using the command line interface (CLI).
See docs:
http://flask.pocoo.org/docs/0.12/shell/
I have just moved from flask-sqlalchemy to just sqlalchemy. And I have started getting below issue.
Issue 1 :
I have two models
class Candidate(db.Model):
__tablename__ = 'candidate'
id = db.Column(db.Integer, primary_key=True)
country_id = db.Column(db.Integer, db.ForeignKey('country.id'), nullable=True)
first_name = db.Column(db.String(100), nullable=True)
last_name = db.Column(db.String(100))
email = db.Column(db.String(255), nullable=False, unique=True)
is_deleted = db.Column(db.Boolean, nullable=False, default=False, server_default=db.text("false"))
class Country(db.Model):
__tablename__ = 'country'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150), nullable=False, unique=True)
created_on = db.Column(db.DateTime, default=_get_date_time)
modified_on = db.Column(db.TIMESTAMP, onupdate=_get_date_time, nullable=False, server_default=db.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))
is_deleted = db.Column(db.Boolean, nullable=False, default=False, server_default=db.text("false"))
I created tables in my students db using alembic. The migration file it generated looks like
# ... snip ...
op.create_table('country',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=150), nullable=False),
sa.Column('created_on', sa.DateTime(), nullable=True),
sa.Column('modified_on', sa.TIMESTAMP(), server_default=sa.text(u'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), nullable=False),
sa.Column('is_deleted', sa.Boolean(), server_default=sa.text(u'false'), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('candidate',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('first_name', sa.String(length=100), nullable=True),
sa.Column('last_name', sa.String(length=100), nullable=True),
sa.Column('country_id', sa.Integer(), nullable=True),
sa.Column('email', sa.String(length=255), nullable=False),
sa.Column('is_deleted', sa.Boolean(), server_default=sa.text(u'false'), nullable=False),
sa.ForeignKeyConstraint(['country_id'], ['country.id'], ),
sa.UniqueConstraint('email'),
)
I loaded data from the mysqldump that I had created when I was using falsk-sqlalchemy.
INSERT INTO `country` VALUES (1,'India','2015-05-16 16:51:30','2015-05-16 11:21:30',0),(2,'Australia','2015-05-16 17:10:33','2015-05-16 11:40:33',0);
INSERT INTO `candidate` VALUES (1,1,'Jon','Snow','jon-snow#got.com',0);
Issue : But mysqldump should have put false in place of is_deleted, don't you think? Also note that when I manually replace 0 by false in mysqldump for is_deleted, I don't fall into Issue 2.
Issue 2 :
I use flask restless API layer to do CRUD operations.
# ... snip ...
manager = flask.ext.restless.APIManager(app, session=mysession)
manager.create_api(Country, methods=['GET'])
manager.create_api(Candidate, methods=['GET','POST'])
# ... snip ...
Issue : When I hit /candidate api, I get below error. It was working fine with flask-sqlalchemy
ValueError: int_to_boolean only accepts None, 0 or 1
Traceback for Issue 2
Traceback (most recent call last):
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_restless/views.py", line 157, in decorator
return func(*args, **kw)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/mimerender.py", line 229, in wrapper
result = target(*args, **kwargs)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask/views.py", line 149, in dispatch_request
return meth(*args, **kwargs)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_restless/views.py", line 189, in wrapped
return func(*args, **kw)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_restless/views.py", line 1239, in get
return self._search()
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_restless/views.py", line 1194, in _search
result = self._paginated(result, deep)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/flask_restless/views.py", line 981, in _paginated
for x in instances[start:end]]
File "/home/hussain/workspace/api/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2228, in __getitem__
return list(res)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 73, in instances
rows = [process[0](row, None) for row in fetch]
File "/home/hussain/workspace/api/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 455, in _instance
populate_state(state, dict_, row, isnew, only_load_props)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 305, in populate_state
populator(state, dict_, row)
File "/home/hussain/workspace/api/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py", line 169, in fetch_col
dict_[key] = row[col]
ValueError: int_to_boolean only accepts None, 0 or 1
How do I tackle this issue? Please help me out.