Updated GORM V2 Release Note Draft (markdown)

Jinzhu 2020-06-17 11:57:59 +08:00
parent c7b5fa8296
commit 72ae6d84a9

@ -1,8 +1,8 @@
# GORM 2.0 Release Note (Draft)
GORM 2.0 is rewrite from scratch based on feedbacks we received in the last few years, it introduces some incompatible-API changes.
GORM 2.0 is rewritten from scratch based on feedback we received in the last few years, it introduces some incompatible-API change.
GORM 2.0 is not yet released, currently in the public beta stage, it has been used stably in few production services, but we are still actively collecting user suggestions, feedbacks to achieve a better GORM V2 before the final release, If everything goes well, the final release date will be the time we reach 20k stars!
GORM 2.0 is not yet released, currently, in the public beta stage, it has been used stably in few production services, but we are still actively collecting user suggestions, feedbacks to achieve a better GORM V2 before the final release, If everything goes well, the final release date will be the time we reach 20k stars!
(The release note is still work-in-progress, it contains most major changes, for details, please checkout http://gorm.io when we finish its rewritten)
@ -14,7 +14,7 @@ GORM 2.0 is not yet released, currently in the public beta stage, it has been us
* Association improvements, Modify Join Table for Many2Many, Association Mode for batch data
* SQL Builder, Upsert, Locking, Optimizer/Index/Comment Hints supports
* Multiple fields support for auto creating/updating time, which also support unix (nano) seconds
* Field permissions supports: readonly, writeonly, createonly, updateonly, ignored
* Field permissions support: readonly, writeonly, createonly, updateonly, ignored
* All new Migrator, Logger
* Naming strategy (Unified table name, field name, join table name, foreign key, checker, index name rule)
* Better customized data type support (e.g: JSON)
@ -79,7 +79,7 @@ tx.Model(&user).Update("role", "admin")
### Batch Insert
* Just pass slice data to `Create`, GORM will generates a single SQL statement to insert all the data and backfill primary key values
* Just pass slice data to `Create`, GORM will generate a single SQL statement to insert all the data and backfill primary key values
* If the data contains associations, all new association objects will be inserted with another SQL
* Batch inserted data will call its `Hooks` methods (Before/After Create/Save)
@ -130,7 +130,7 @@ stmt.Vars //=> []interface{}{1}
### Join Preload
* `Preload` loads the association data in a separate query, `Join Preload` will loads association data using inner join
* It can handles null association
* It can handle null association
```go
DB.Joins("Company").Joins("Manager").Joins("Account").First(&user, 1)
@ -181,10 +181,10 @@ result.RowsAffected // number of records in all batches
* Easier to use `tag` to specify foreign keys (when not following the naming convention)
```go
// Belongs To: `ForeignKey` specifies foreign key field owned by current model, `References` specifies association's primary key
// Has One/Many: `ForeignKey` specifies foreign key for association, `References` specifies current model's primary key
// Many2Many: `ForeignKey` specifies current model's primary key, `JoinForeignKey` specifies join table's foreign key that refer to `ForeignKey`
// `References` specifies association's primary key, `JoinReferences` specifies join table's foreign key that refer to `References`
// Belongs To: `ForeignKey` specifies foreign key field owned by the current model, `References` specifies the association's primary key
// Has One/Many: `ForeignKey` specifies foreign key for the association, `References` specifies the current model's primary key
// Many2Many: `ForeignKey` specifies the current model's primary key, `JoinForeignKey` specifies join table's foreign key that refers to `ForeignKey`
// `References` specifies the association's primary key, `JoinReferences` specifies join table's foreign key that refers to `References`
// For multiple foreign keys, it can be separated by commas
type Profile struct {
@ -222,7 +222,7 @@ type Blog struct {
### Modify Join Table for Many2Many
Eaiser to setup Many2Many's `JoinTable`the `JoinTable` can be a full-featured model, like having `Soft Delete``Hooks` supports, and define more fields
Easier to setup Many2Many's `JoinTable`the `JoinTable` can be a full-featured model, like having `Soft Delete``Hooks` supports, and define more fields
```go
type Person struct {
@ -274,7 +274,7 @@ gorm.Model(&users).Association("Team").Append(&userA, &userB, &[]User{userA, use
gorm.Model(&users).Association("Team").Replace(&userA, &userB, &[]User{userA, userB, userC})
```
### Multiple fields support for auto creating/updating time, which also support unix (nano) seconds (when data type is int)
### Multiple fields support for auto creating/updating time, which also support unix (nano) seconds (when the data type is int)
```go
type User struct {
@ -285,7 +285,7 @@ type User struct {
}
```
### Field permissions supports: readonly, writeonly, createonly, updateonly, ignored
### Field permissions support: readonly, writeonly, createonly, updateonly, ignored
```go
type User struct {
@ -300,9 +300,9 @@ type User struct {
### All new Migrator
* Migrator will creates database foreign keys (and ignore or delete foreign key constraint when `DropTable`)
* Migrator is more independent, providing better support for each database and unified API interfaces. we can design better migrate tools based on it (for example: sqlite doesn't support `ALTER COLUMN`, `DROP COLUMN`, GORM will create a new table as the one you are trying to change, copy all data, drop old table, rename the new table)
* Support to set `check` constraint through tag
* Migrator will create database foreign keys (and ignore or delete foreign key constraint when `DropTable`)
* Migrator is more independent, providing better support for each database and unified API interfaces. we can design better migrate tools based on it (for example SQLite doesn't support `ALTER COLUMN`, `DROP COLUMN`, GORM will create a new table as the one you are trying to change, copy all data, drop the old table, rename the new table)
* Support to set `check` constraint through the tag
* Enhanced tag setting for `index`
```go
@ -317,7 +317,7 @@ type UserIndex struct {
### Naming Strategy
You can setup naming strategy (table name, field name, join table name, foreign key, checker, index name) during initialization, which is convenient to modify the naming rules (such as adding table name prefix), for details, please refer to: [https://github.com/go-gorm/gorm/blob/master/schema/naming.go#L14](https://github.com/go-gorm/gorm/blob/master/schema/naming.go#L14)
You can setup naming strategy (table name, field name, join table name, foreign key, checker, index name) during initialization, which is convenient to modify the naming rules (such as adding table name prefix), for details, please refer to [https://github.com/go-gorm/gorm/blob/master/schema/naming.go#L14](https://github.com/go-gorm/gorm/blob/master/schema/naming.go#L14)
```go
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
@ -331,7 +331,7 @@ In addition to the context support mentioned above, Logger has also optimized th
* Customize/turn off the colors in the log
* Slow SQL log, default slow SQL time is 100ms
* Optimized the SQL log format so that it can be copied and executed in database console
* Optimized the SQL log format so that it can be copied and executed in a database console
### Safely update, delete. do update/delete without any conditions would be prohibited
@ -391,9 +391,9 @@ func (User) TableName() string {
### Better customized data type support (JSON as an example)
GORM optimizes support for custom types, so you can define data structure to support all databases
GORM optimizes support for custom types, so you can define a data structure to support all databases
The following takes JSON as an example (supported mysql, postgres, refer: https://github.com/go-gorm/datatypes/blob/master/json.go)
The following takes JSON as an example (supported MySQL, Postgres, refer: https://github.com/go-gorm/datatypes/blob/master/json.go)
```go
import "gorm.io/datatypes"
@ -418,7 +418,7 @@ DB.First(&user, datatypes.JSONQuery("attributes").HasKey("orgs", "orga"))
### Omit, Select optimizes
```go
// When creating object
// When creating objects
DB.Select("Name", "Age").Create(&user) // Using the fields specified by Select
DB.Omit([]string{"Name", "Age"}).Create(&user) // Ignoring the field specified by Omit
@ -429,13 +429,13 @@ DB.Model(&user).Omit([]string{"Role"}).Update(User{Name: "jinzhu", Role: "admin"
### SQL Builder
* GORM uses SQL Builder generate `SQL` internally
* GORM uses SQL Builder generates `SQL` internally
* When performing an operation, GORM creates a `Statement` object, all APIs add/change `Clause` for the `Statement`, at last, GORM generated SQL based on those clauses
* For different databases, Clauses may generate different SQL
#### Upsert
`clause.OnConflict` provides compatible Upsert support for different databases (Sqlite, MySQL, PostgreSQL, SQL Server)
`clause.OnConflict` provides compatible Upsert support for different databases (SQLite, MySQL, PostgreSQL, SQL Server)
```go
import "gorm.io/gorm/clause"