我有禁令和帐户的数据库关系。一个帐户可以有多个禁令。我的课程如下所示:
class Account(Base):
__tablename__ = "accounts"
_accountId = Column(Integer, name="accountId", primary_key=True)
_username = Column(String, name="username")
_password = Column(String, name="password")
_salt = Column(String, name="salt")
_lastLoggedIn = Column(String, name="lastLoggedIn")
_banned = relationship("Banned")
class Banned(Base):
__tablename__ = 'bannedAccounts'
_id = Column(Integer, name="Id", primary_key=True)
_accountId = Column(Integer, ForeignKey('accounts.accountId'), name="accountId")
_reason = Column(String, name="Reason")
_expires = Column(String, name="Expires")
我正在尝试进行一个查询,该查询涉及选择所有具有用户名的帐户并将其加入被禁止的表中,以查看它们是否有任何禁令。我的代码目前如下所示:
engine = create_engine(self.connectionString)
Session = sessionmaker(bind=engine)
session = Session()
query = session.query(Account, Banned)
query = query.filter(Account._username == self.username).filter(Account._accountId == Banned._accountId).all()
self.username
我的问题是,只要他们有一些禁令,它就会返回一组帐户及其相关的禁令。但是,如果他们没有任何禁令,而不是让帐户对象归还,我就会被None
归还。有没有什么办法解决这一问题?
编辑我想在sql中到达的终点是这个
SELECT *
FROM accounts
LEFT JOIN bannedaccounts
ON accounts.accountId = bannedaccounts.AccountID
WHERE username= self.username