Код: Выделить всё
from sqlalchemy.orm import DeclarativeBase
class Place(DeclarativeBase):
__tablename__ = "places"
other_storage_key: Mapped[list[str] | None] = mapped_column(ARRAY(String), nullable=True)
def update_place_other_storage_key_by_place_code(self, place_code: int, other: str) -> None:
session = Session()
query = select(Place).where(Place.place_code == place_code)
result = session.execute(query)
place = result.scalars().first()
if place:
if place.other_storage_key:
place.other_storage_key.append(other)
else:
place.other_storage_key = [other]
session.add(place)
session.commit()
Код: Выделить всё
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.orm import DeclarativeBase
class Place(DeclarativeBase):
__tablename__ = "places"
other_storage_key: Mapped[list[str] | None] = mapped_column(MutableDict.as_mutable(ARRAY(String)), nullable=True)
"/usr/local/lib/python3.12/site-packages/sqlalchemy/ext/mutable.py",
строка 860, принудительная
return Mutable.coerce(key, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Файл "/usr/local/lib/python3.12/site-packages/sqlalchemy/ext/mutable .py",
строка 455, принудительно
поднимите ValueError(msg % (key, type(value))) ValueError: атрибут 'other_storage_key' не принимает объекты введите
Подробнее здесь: https://stackoverflow.com/questions/793 ... -the-array
Мобильная версия