Skip to content

Instantly share code, notes, and snippets.

@comargo
Created November 15, 2021 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comargo/0ca997e45ce2d59ca456f556c6d5a07d to your computer and use it in GitHub Desktop.
Save comargo/0ca997e45ce2d59ca456f556c6d5a07d to your computer and use it in GitHub Desktop.
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