I am trying to create and fill a table in SQLite through SQLAlchemy using declarative_base()
Here is table declaration:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
Base = declarative_base()
class Persons(Base):
__tablename__ = 'persons'
entry_id = Column(Integer, primary_key=True)
age = Column(Integer)
gender = Column(String(6))
Then engine and session:
engine = create_engine(r'sqlite:///persons.db')
db_session = scoped_session(sessionmaker(bind=engine))
session = db_session()
Table creation:
Base.metadata.create_all(engine)
It all works fine, until I try to do bulk mappings insert:
session.bulk_insert_mappings(table, bulk_rows)
If i use table = Persons
for this, it all works fine and insert works as indented. But if I use table = Base.metadata.tables['persons']
, which returns table object from Base, then it throws an error:
AttributeError: 'Table' object has no attribute 'mapper'
Why is that happening and can I somehow use Base.metadata.tables['persons']
table object for bulk inserts?