目錄
方式一
方式二
首頁 後端開發 Python教學 python sqlalchemy動態修改tablename實作方式有哪些

python sqlalchemy動態修改tablename實作方式有哪些

Apr 28, 2023 am 09:58 AM
python sqlalchemy

方式一

在Python的SQLAlchemy ORM中,您可以使用下列程式碼動態地變更資料模型類別的表名:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class MyModel(Base):
    __tablename__ = 'my_custom_table_name'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    age = Column(Integer)
#... 其它定义或使用...
# 动态修改表名
MyModel.__table__.name = 'my_new_table_name'
登入後複製

在這個範例中,我們先定義了一個名為MyModel的資料模型,並將其與my_custom_table_name表相關聯。然後,在必要時,我們可以透過存取模型類別的__table__屬性來動態地變更表名。

這是因為ORM框架本身會自動為每個資料模型類別建立一個對應的Table對象,並將其儲存在該類別的__table__屬性中。我們可以使用name屬性直接更新此物件的名稱,從而實現動態更改表名的目的。

請注意,動態變更表名可能會影響到您的應用程式的正確性和可維護性。因此,請考慮清楚是否真正需要這樣做,以及如何規劃資料庫模式的變更。

方式二

如果我們原先的class為此:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class MyModel(Base):
    __tablename__ = 'my_custom_table_name'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    age = Column(Integer)
登入後複製

我們可以定義其抽象類別:

class MyModelCls(Base):
    __abstract__ = True  # 关键语句,定义所有数据库表对应的父类
    __table_args__ = {"extend_existing": True}  # 允许表已存在
    id = Column(Integer, primary_key=True)
    name = Column(String)
    age = Column(Integer)
登入後複製

然後透過函數傳回class物件

def get_source_data_model_cls(cid, cid_class_dict={}):
    if cid not in cid_class_dict:
        cls_name = table_name = cid
        cls = type(cls_name, (MyModelCls, ), {'__tablename__': table_name})
        cid_class_dict[cid] = cls
    return cid_class_dict[cid]
登入後複製

以上是python sqlalchemy動態修改tablename實作方式有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

模板化的優點和缺點有哪些? 模板化的優點和缺點有哪些? May 08, 2024 pm 03:51 PM

模板化的優點和缺點有哪些?

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Jul 01, 2024 am 07:22 AM

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2

怎麼下載deepseek 小米 怎麼下載deepseek 小米 Feb 19, 2025 pm 05:27 PM

怎麼下載deepseek 小米

deepseek怎麼問他 deepseek怎麼問他 Feb 19, 2025 pm 04:42 PM

deepseek怎麼問他

NET40是什麼軟體 NET40是什麼軟體 May 10, 2024 am 01:12 AM

NET40是什麼軟體

deepseek該怎麼搜索 deepseek該怎麼搜索 Feb 19, 2025 pm 05:18 PM

deepseek該怎麼搜索

瀏覽器插件是什麼語言寫的 瀏覽器插件是什麼語言寫的 May 08, 2024 pm 09:36 PM

瀏覽器插件是什麼語言寫的

deepseek怎麼編程 deepseek怎麼編程 Feb 19, 2025 pm 05:36 PM

deepseek怎麼編程

See all articles