<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 69 alembic數據庫遷移工具基本使用 alembic是sqlalchemy的作者開發的。用來做ORM模型與數據的遷移與映射。alembic使用方式跟git有點類似,表現在兩個方面,第一個,alembic的所有命令都是以alembic開頭,第二,alembic的遷移文件也是通過版本進行控制的。首先,通過pip install alembic進行安裝。以下將解釋alembic的用法: 1.初始化alembic倉庫:在終端中,cd到項目目錄中,然后執行alembic init alembic,創建一個名叫alembic的倉庫。 ```text alembic init learn_alembic ``` 2.創建模型類:創建一個models.py模塊,然后在里面定義模型類,示例代碼: ```text from sqlalchemy import create_engine,Column,String,Float,Integer from sqlalchemy.ext.declarative import declarative_base # from sqlalchemy.orm import sessionmaker HOSTNAME = "127.0.0.1" PORT = "3306" DATABASE = "alembic_demo" USERNAME = "root" PASSWORD = "123456" # dialect+dricer://username:password@host:port/database DB_URI = "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE) engine = create_engine(engine) Base = declarative_base(engine) # session = sessionmaker(engine)() class User(Base): __tablename__ = "user" id = Column(Integer,primary_key=True,autoincrement=True) username = Column(String(50),nullable=False) ``` 3.修改配置文件,指定連接的數據庫: * 在alembic.ini中設置數據庫的連接,sqlalchemy.url = driver://user:pass@localhost/dbname,比較以mysql數據庫為例,則配置后的代碼為: * ```text sqlalchemy.url = mysql+pymysql://root:123456@localhost/alembic_demo?charset=utf8 ``` * 為了使用模型類更新數據庫,需要在env.py文件中設置target\_metadata,默認為target\_metadata=None。使用sys模塊把當前項目的路徑導入到path中: * ```text import sys,os # 1.__file__:當前文檔(env.py) #2.os.path.dirname(__file__):獲取當前文檔的目錄 #3.os.path.dirname(os.path.dirname(__file__)):獲取當前文檔目錄的上一級目錄 #4.sys.path: python尋找導入的包的所有路徑 sys.path.append(os.path.dirname(os.path.dirname(__file__))) import models ...# 省略代碼 target_metadata = Base.metadata # 設置創建模型的元類 ...# 省略代碼 ``` ![](https://box.kancloud.cn/13587428bc05639ea9c9bced4654d261_391x472.png) 4.自動生成遷移文件:使用alembic revision --autogenerate -m "message" 將當前模型中的狀態生成遷移文件 ```text alembic revision --autogenerate -m "第一次提交" ``` ![](https://box.kancloud.cn/642dc732b983354d58b34b23741cd29b_348x107.png) 5.更新數據庫:使用alembic upgrade head 將剛剛生成的遷移文件,真正映射到數據庫中。 ```text alembic upgrade head ``` 6.修改代碼后,重復4~5步驟
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看