sqlalchemy orm-从数据库获取表定义

发布于 2025-02-09 12:53:48 字数 390 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只有影子陪我不离不弃 2025-02-16 12:53:48

在sqlalchemy 中。

我将在此处放置受文档启发的示例。

1。给班级
from sqlalchemy import Table, create_engine
from sqlalchemy.orm import declarative_base

Base = declarative_base()  # same declarative_base() as usual

engine = create_engine("sqlite:///mydatabase.db")  # get your engine

class User(Base):
    __table__ = Table("user", Base.metadata, autoload_with=engine)
2。用 automap扩展
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base

Base = automap_base()  # rather than declarative_base()

engine = create_engine("sqlite:///mydatabase.db")  # get your engine

Base.prepare(autoload_with=engine)  # reflect the tables and classes

User = Base.classes.user  # retrieve classes from table name
3。从 deferredreflection扩展
from sqlalchemy.ext.declarative import DeferredReflection
from sqlalchemy.orm import declarative_base

Base = declarative_base()  # same declarative_base() as usual

class User(DeferredReflection, Base):  # define your class
    __tablename__ = "user"

engine = create_engine("sqlite:///mydatabase.db")  # get your engine

User.prepare(engine)  # reflect the table and attributes

两个第一个示例需要引擎来创建类,这可能会带来不方便的测试等,因此,我因此更喜欢让我可以在不同地方定义的第三个,但三个都可以使用。

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__
from sqlalchemy import Table, create_engine
from sqlalchemy.orm import declarative_base

Base = declarative_base()  # same declarative_base() as usual

engine = create_engine("sqlite:///mydatabase.db")  # get your engine

class User(Base):
    __table__ = Table("user", Base.metadata, autoload_with=engine)
2. Retrieving classes with the Automap extension
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base

Base = automap_base()  # rather than declarative_base()

engine = create_engine("sqlite:///mydatabase.db")  # get your engine

Base.prepare(autoload_with=engine)  # reflect the tables and classes

User = Base.classes.user  # retrieve classes from table name
3. Inheriting from the DeferredReflection extension
from sqlalchemy.ext.declarative import DeferredReflection
from sqlalchemy.orm import declarative_base

Base = declarative_base()  # same declarative_base() as usual

class User(DeferredReflection, Base):  # define your class
    __tablename__ = "user"

engine = create_engine("sqlite:///mydatabase.db")  # get your engine

User.prepare(engine)  # reflect the table and attributes

The 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文