SELECT *在SQLAlchemy中可以做到吗?
具体来说,SELECT * WHERE foo=1?
我有一个用表映射的类,在我的例子中是以声明的方式,我想从这个类中"发现"表属性,列,名称,关系:
engine = create_engine('sqlite:///' + databasePath, echo=True)
# setting up root class for declarative declaration
Base = declarative_base(bind=engine)
class Ship(Base):
__tablename__ = 'ships'
id = Column(Integer, primary_key=True)
name = Column(String(255))
def __init__(self, name):
self.name = name
def __repr__(self):
return "<Ship('%s')>" % (self.name)
Run Code Online (Sandbox Code Playgroud)
所以现在我的目标是从"Ship"类中获取另一段代码中的表列及其属性.我想我可以使用检测来处理它,但SQLAlchemy API是否提供了任何方法?
我想克隆一个sqlalchemy对象:
我试过了
product_obj = products.all()[0] #here products is service name
product_obj.product_uid = 'soemthing' #here product_uid is the pk of product model
products.save(product_obj)
Run Code Online (Sandbox Code Playgroud)
它只是更新old_object
这是products.save函数的代码
class Service(object):
__model__ = None
def save(self, model):
self._isinstance(model)
db.session.add(model)
db.session.commit()
return model
Run Code Online (Sandbox Code Playgroud)