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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # GORM 配置 GORM 提供的配置可以在初始化時使用 ```go type Config struct { SkipDefaultTransaction bool NamingStrategy schema.Namer Logger logger.Interface NowFunc func() time.Time DryRun bool PrepareStmt bool DisableAutomaticPing bool DisableForeignKeyConstraintWhenMigrating bool } ``` ## 跳過默認事務 為了確保數據一致性,GORM 會在事務里執行寫入操作(創建、更新、刪除)。如果沒有這方面的要求,您可以在初始化時禁用它。 ```go db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ SkipDefaultTransaction: true, }) ``` ## 命名策略 GORM 允許用戶通過覆蓋默認的`命名策略`更改默認的命名約定,這需要實現接口 `Namer` ```go type Namer interface { TableName(table string) string ColumnName(table, column string) string JoinTableName(table string) string RelationshipFKName(Relationship) string CheckerName(table, column string) string IndexName(table, column string) string } ``` 默認 `NamingStrategy` 也提供了幾個選項,如: ```go db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ NamingStrategy: schema.NamingStrategy{ TablePrefix: "t_", // 表名前綴,`User` 的表名應該是 `t_users` SingularTable: true, // 使用單數表名,啟用該選項,此時,`User` 的表名應該是 `t_user` }, }) ``` ## Logger 允許通過覆蓋此選項更改 GORM 的默認 logger,參考 [Logger](http://v2.gorm.io/zh_CN/docs/logger.html) 獲取詳情 ## NowFunc 更改創建時間使用的函數 ```go db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ NowFunc: func() time.Time { return time.Now().Local() }, }) ``` ## DryRun 生成 `SQL` 但不執行,可以用于準備或測試生成的 SQL,參考 [會話](http://v2.gorm.io/zh_CN/docs/session.html) 獲取詳情 ```go db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ DryRun: false, }) ``` ## PrepareStmt `PreparedStmt` 在執行任何 SQL 時都會創建一個 prepared statement 并將其緩存,以提高后續的效率,參考 [會話](http://v2.gorm.io/zh_CN/docs/session.html) 獲取詳情 ```go db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ PrepareStmt: false, }) ``` ## DisableAutomaticPing 在完成初始化后,GORM 會自動 ping 數據庫以檢查數據庫的可用性,若要禁用該特性,可將其設置為 `true` ```go db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ DisableAutomaticPing: true, }) ``` ## DisableForeignKeyConstraintWhenMigrating 在 `AutoMigrate` 或 `CreateTable` 時,GORM 會自動創建外鍵約束,若要禁用該特性,可將其設置為 `true`,參考 [遷移](http://v2.gorm.io/zh_CN/docs/migration.html) 獲取詳情。 ```go db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{ DisableForeignKeyConstraintWhenMigrating: true, }) ```
                  <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>

                              哎呀哎呀视频在线观看