change(fastapi): unify all model relationship behavior

Now, we allow the direct relationships and their foreign keys to
be set in all of our models. Previously, we constrained this to
direct relationships, and this forced users to perform a query
in most situations to satisfy that requirement. Now, IDs can be
passed directly.

Additionally, this change removes the need for extraneous imports
when users which to use relationships. We now import and use models
directly instead of passing string-references to them.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-16 16:16:44 -07:00
parent 0c37216626
commit 51320ab22a
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
24 changed files with 181 additions and 297 deletions

View file

@ -13,6 +13,7 @@ import aurweb.models.account_type
import aurweb.schema
from aurweb import db
from aurweb.models.account_type import AccountType as _AccountType
from aurweb.models.ban import is_banned
from aurweb.models.declarative import Base
@ -29,7 +30,7 @@ class User(Base):
Integer, ForeignKey("AccountTypes.ID", ondelete="NO ACTION"),
nullable=False, server_default=text("1"))
AccountType = relationship(
"AccountType",
_AccountType,
backref=backref("users", lazy="dynamic"),
foreign_keys=[AccountTypeID],
uselist=False)