Created
November 15, 2021 19:49
-
-
Save comargo/0ca997e45ce2d59ca456f556c6d5a07d to your computer and use it in GitHub Desktop.
SQLAlchemy question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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