4

I am working on a prototype to pull stocks ticks and dump them to a DB. I want to pass __tablename__ as a parameter to SQLAchemy so that stock ticks for a given stocks gets written down to its own table. (BTW, I am new to SQLAlchemy)

I referred this thread: Python Sqlalchemy - tablename as a variable

And came up with below code:

from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy import Column, String, Integer, Date Base = declarative_base() def create_models(tablename): class Stock(Base): __tablename__ = tablename timestamp = Column(String, primary_key=True) ltp = Column(String) def __init__(self, timestamp, ltp): self.timestamp = timestamp self.ltp = ltp create_models('ABCD') engine = create_engine('sqlite:////ticks.db') Base.metadata.create_all(bind=engine) session_factory = sessionmaker(bind=engine) Session = scoped_session(session_factory)() tick = Stock('2019-02-12 09:15:00', '287') Session.merge(tick) Session.commit() 

But it fails:

Traceback (most recent call last): File "quickies.py", line 32, in <module> tick = Stock('2019-02-12 09:15:00', '287') NameError: name 'Stock' is not defined 

The error is quite obvious. But then I am unsure how to proceed with __tablename__ as a variable. Any pointers would be of great help.

9
  • Does the table exist already? Commented Feb 12, 2019 at 9:00
  • No. I believe Base.metadata.create_all(bind=engine) this line is creating the table. If I remove the create_models function and run the code by hardcoding __tablename__, the code works fine. It creates table on 1st run and on subsequent runs, it continues to inserts the records. Commented Feb 12, 2019 at 9:03
  • 1
    take class Stock out side of function create_models Commented Feb 12, 2019 at 9:03
  • @ChetanAmeta, If I take class Stock out side of create_models, then how do I pass __tablename__ as a variable. Commented Feb 12, 2019 at 9:06
  • 2
    use return Stock in your function and then Stock = create_models('ABCD') then tick = Stock('2019-02-12 09:15:00', '287') Commented Feb 12, 2019 at 9:11

1 Answer 1

10

The scope of your Stock class limited to create_models function. To create object of this class outside the function you can return the class from the function and then use it.

have a look on below solution:

def create_models(tablename): class Stock(Base): __tablename__ = tablename timestamp = Column(String, primary_key=True) ltp = Column(String) def __init__(self, timestamp, ltp): self.timestamp = timestamp self.ltp = ltp return Stock #return the class Stock = create_models('ABCD') tick = Stock('2019-02-12 09:15:00', '287') 

have a look at Python scope tutorial for more detail related to scope.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.