Consider the following SQLAlchemy code
class Foo(Base):
__tablename__ = 'currency'
id = Column(Integer, primary_key=True)
name = Column(String(40), nullable=False, unique=True)
symbol = Column(String(4), nullable=False, unique=True)
l = session.query(Foo.symbol, Foo.id).all()
In the last line, I am trying to generate a list of the symbol-id pairs. It produces the following error:
NameError: name 'Foo' is not defined
I tried the following and got the errors as specified:
l = session.query(models.Foo.symbol, models.Foo.id).all()
#note: The Foo object is in the models.py file
#error: NameError: name 'models' is not defined
l = session.query(symbol, id).all()
#error: sqlalchemy.exc.CompileError: Cannot compile Column object until its 'name' is assigned.
l = session.query(self.symbol, self.id).all()
#error: NameError: name 'self' is not defined
So how do I pass in Foo object's column names to the SqlAlhemy query from inside the Foo class?
Why am I doing this? I am then converting the list to a dictionary and only access the dictionary from code throughout the program since its values are seldom changed. So I want to populate it once and then access it many many times without hitting the DB again. And I want to keep it in the Foo Class which I believe is where it belongs.
If there is a better way to do this please do let me know.
Foo.lto be updated? Magic?