Skip to content

Instantly share code, notes, and snippets.

@comargo
Created November 15, 2021 19:49
Embed
What would you like to do?
SQLAlchemy question
import sqlalchemy as sa
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()
class A(Base):
__tablename__ = 'A'
id = sa.Column(sa.Integer, primary_key=True)
def __repr__(self) -> str:
return f'<A(id={self.id})>'
class B(Base):
__tablename__ = 'B'
id = sa.Column(sa.Integer, primary_key=True)
a_id = sa.Column(sa.Integer, sa.ForeignKey(A.id))
a = relationship(A, backref="bs")
def __repr__(self) -> str:
return f'<B(id={self.id}, a={self.a})>'
def check():
try:
A.bs
print("A.bs ok")
except AttributeError as e:
print(e)
check()
A()
check()
type object 'A' has no attribute 'bs'
A.bs ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment