sqlalchemy orm-从数据库获取表定义
Sqlalchemy,是否有一种直接创建表定义类的方法,BeaCause表已在DB中存在,大多数文档都告诉我,将其定义进行人工制作。
Base = declarative_base()
# ORM defininition
class Dep(Base):
__tablename__='dep'
id=Column(Integer,primary_key=True,autoincrement=True)
dname=Column(String(64),nullable=False,index=True)
# Add new obs
session = sessionMaker()
row_obj=Dep(dname='saleman')
session.add(row_obj)
SQLAlchemy, Is there a way to create table definition class directly, beacause the table has already existed in DB, most documents tell me to make the definition mannually.
Base = declarative_base()
# ORM defininition
class Dep(Base):
__tablename__='dep'
id=Column(Integer,primary_key=True,autoincrement=True)
dname=Column(String(64),nullable=False,index=True)
# Add new obs
session = sessionMaker()
row_obj=Dep(dname='saleman')
session.add(row_obj)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有在sqlalchemy 中。
我将在此处放置受文档启发的示例。
1。给班级
2。用 automap扩展
3。从
deferredreflection
扩展两个第一个示例需要引擎来创建类,这可能会带来不方便的测试等,因此,我因此更喜欢让我可以在不同地方定义的第三个,但三个都可以使用。
There are several of ways to reflect tables in SQLAlchemy.
I'll put here examples inspired by the documentation.
1. Giving the class an autoloaded
__table__
2. Retrieving classes with the Automap extension
3. Inheriting from the
DeferredReflection
extensionThe two first examples need an engine to create the class, which can be inconvenient for testing and such, so I prefer the third one which allows me to have class and engine defined in different places, but all three will work.