fix(fastapi): support UsersID and User columns in the Session model

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-30 20:12:06 -08:00
parent 31a093ba06
commit a0e1a1641d
No known key found for this signature in database
GPG key ID: F7E46DED420788F3

View file

@ -18,10 +18,16 @@ class Session(Base):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
user_exists = db.query( # We'll try to either use UsersID or User.ID if we can.
db.query(_User).filter(_User.ID == self.UsersID).exists() # If neither exist, an AttributeError is raised, in which case
).scalar() # we set the uid to 0, which triggers IntegrityError below.
if not user_exists: try:
uid = self.UsersID or self.User.ID
except AttributeError:
uid = 0
user_exists = db.query(_User).filter(_User.ID == uid).exists()
if not db.query(user_exists).scalar():
raise IntegrityError( raise IntegrityError(
statement=("Foreign key UsersID cannot be null and " statement=("Foreign key UsersID cannot be null and "
"must be a valid user's ID."), "must be a valid user's ID."),