Green: implement custom object for arrays, update is ok

This commit is contained in:
Colin Goutte 2023-08-26 22:51:05 +02:00
parent 68e11a3275
commit 0ac827b95c
2 changed files with 20 additions and 2 deletions

1
dev.py
View File

@ -104,6 +104,7 @@ async def update_movie(
async def get_movie(id_: str, db: Session = Depends(get_db)):
try:
movie = crud.get_movie_by_id(db, id_)
out = {k: v for (k, v) in movie.__dict__.items() if not k.startswith("_")}
except LookupError:
raise HTTPException(status_code=404, detail=f"No movie found with id {id_}")

View File

@ -1,5 +1,20 @@
from sqlalchemy import Column, ForeignKey, Integer, String, Float
from sqlalchemy import Column, ForeignKey, Integer, String, Float, types
from database import Base
import sqlalchemy.types as types
class NaiveStringList(types.TypeDecorator):
impl = types.Unicode
sep = "\n"
def process_bind_param(self, value, dialect):
return self.sep.join(value)
def process_result_value(self, value, dialect):
return value.split(self.sep)
def copy(self, **kw):
return NaiveStringList(self.impl.length)
class Movie(Base):
@ -12,6 +27,8 @@ class Movie(Base):
vote_count = Column(Integer)
vote_average = Column(Float)
genres = Column(String) # String array dimention 1
genres = Column(NaiveStringList) # LLw
# genres = Column(ARRAY(String, dimensions=1)) # String array dimention 1
description = Column(String)
release_date = Column(String)