from sqlalchemy import Column, Integer, String, MetaData, Table, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
#table columns describing
params = dict( lid = Column(Integer, primary_key=True),
cab_name = Column(String))
table_name = dict(__tablename__ = "table")
def cab_init(self, **kwargs ):
for k,v in kwargs.items():
setattr(self, k, v)
methods = dict( __init__ = cab_init )
cab_item = type("cab_item", (declarative_base(),), dict ( methods.items()
+ params.items()
+ table_name.items() ) )
engine = create_engine('sqlite:///:memory:', echo=False)
meta = MetaData()
table = Table('table', meta, #(*1)
Column('lid', Integer,primary_key=True),
Column('cab_name', String),
)
#table = Table(name = 'table', metadata = meta, args = params.values()) #(*2)
meta.create_all(engine)
session = sessionmaker(engine)()
session.merge(cab_item(lid=10, cab_name="blah"))
session.commit()
(* 1)工作正常,(* 2)产生错误
sqlalchemy.exc.OperationalError:(OperationalError)没有这样的表: 表u' SELECT" table" .lid AS table_lid," table" .cab_name AS table_cab_name \ nFROM" table" \ nWHERE" table" .lid =?' (10)
为什么?
答案 0 :(得分:1)
我找到了解决方案
params被宣布错误。正确的方法是
params2 = (Column('lid', Integer, primary_key=True),
Column('cab_name',String))
致电Table
table = Table('table', meta, *params2)
ctor
醇>