go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.96k stars 3.93k forks source link

GORM v2 is on going #2886

Closed jinzhu closed 4 years ago

jinzhu commented 4 years ago

Hello All,

GORM v2 is under active development (https://github.com/jinzhu/gorm/tree/v2_dev), going to release in the next two months.

Before that, I am NOT going to merge any pull requests based on the master branch.

V2 will be overwritten from scratch with similar public API, it would focus on performance, improve usability and fix fragile designs. We will provide a migration guide to help users migrate before the release.

With the new architecture, it opens the possibility to have a better migration tool, a static code generator that generates type-safe code with all the features of GORM, visualizes models and its relationships to help a new team member quickly understand your project, and even support Redis & MongoDB...

Your code review or suggestions would be much appreciated, please comment to this thread, thank you.

vabshere commented 4 years ago

@SujalKokh That is the expected behaviour. Deletes/updates through auto-migrations are not safe and can cause severe damage. You can use DropColumn for dropping a column though.

raygervais commented 4 years ago

Question, I've been rewriting an API backend which utilized Gorm 1.X, and I notice the following doesn't return any associated books. What am I doing wrong? Thanks for any help you can provide, I absolutely love GORM 2's API.

When hitting the API with /series/1 (which is valid and created with the following CURL command: curl -d '{"description":"value1", "title":"value2", "books":[{"title":"Harry Potter", "author": "lol"}]}' -H "Content-Type: application/json" -X POST http://localhost:8000/series), the response is:

{
  "ID":1,
  "CreatedAt":"2020-07-19T10:26:13.948013-04:00",
  "UpdatedAt":"2020-07-19T10:26:13.948013-04:00",
  "DeletedAt": {
    "Time":"0001-01-01T00:00:00Z",
    "Valid":false
  },
  "title":"value2",
  "description":"value1",
  "books":[]
}

Confused because looking into the sqlite database, I see the bridge table and book being created.

type Book struct {
    gorm.Model
    Title  string `json:"title"`
    Author string `json:"author"`
}

type Series struct {
    gorm.Model
    Title       string `json:"title"`
    Description string `json:"description"`
    Books       []Book `json:"books" gorm:"many2many:Series_Book"`
}

func CreateSeries(app *application.Application) gin.HandlerFunc {
    return func(c *gin.Context) {
        var input models.CreateSeriesInput
        if err := c.ShouldBindJSON(&input); err != nil {
            c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
            return
        }

        series := models.Series{Title: input.Title, Description: input.Description, Books: input.Books}

        app.Database.Client.Create(&series)

        c.JSON(http.StatusOK, series)
    }
}

func RetrieveSeriesByID(db *gorm.DB, c *gin.Context, series *models.Series) bool {
    if err := db.Where("id = ?", c.Param("id")).First(&series).Error; err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
        return false
    }

    // Associtation Mode
    if err := db.Model(&series).Association("Books").Error; err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    }

    db.Model(&series).Association("Books").Find(&series.Books)

    return true
}
JoveYu commented 4 years ago

when i write a custom logger, how can i get sql.DB object for get sql.DBStats, i want to log DBStatus.InUse and DBStatus.Idle

wolfgang-braun commented 4 years ago

@jinzhu great work, thanks!

I'm currently trying out v2 (MySQL) and have 3 questions:

jinzhu commented 4 years ago

@jinzhu great work, thanks!

I'm currently trying out v2 (MySQL) and have 3 questions:

  • Won't there be a Close() connection func anymore?
  • I'm reusing a single instance of *gorm.DB (provided by gorm.Open()) for multiple queries and faced unexpected behavior: It seemed like it stored conditions of queries executed before and reused them when executing other queries. Using a *gorm.DB instance from db.Session(&gorm.Session{}) the problem disappeared. Is this how it should be used?
  • Testing the batch inserts I got Error 1390: Prepared statement contains too many placeholders - gorm could execute it in chunks to avoid this, right? Otherwise everybody would have to reimplement the chunk logic..

http://v2.gorm.io/docs/connecting_to_the_database.html#Connection-Pool http://v2.gorm.io/docs/generic_interface.html http://v2.gorm.io/docs/method_chaining.html

jinzhu commented 4 years ago

when i write a custom logger, how can i get sql.DB object for get sql.DBStats, i want to log DBStatus.InUse and DBStatus.Idle

initialize the logger with DB

larry-a4 commented 4 years ago

@jinzhu We are in the process of migrating from gorm V1 to V2, and V2 has been great!

One question though: does V2 Postgres driver support jsonb? I can't seem to find the relevant functions.

friesencr commented 4 years ago

I might be having a bug with Joins. When adding joins to a query it isn't in the query in the same order as v1.

Awezome commented 4 years ago

can I use the last commit in production ?

jinzhu commented 4 years ago

can I use the last commit in production?

There are hundreds of services already using GORM V2 ;)

jinzhu commented 4 years ago

@jinzhu We are in the process of migrating from gorm V1 to V2, and V2 has been great!

One question though: does V2 Postgres driver support jsonb? I can't seem to find the relevant functions.

https://github.com/go-gorm/datatypes

jinzhu commented 4 years ago

I might be having a bug with Joins. When adding joins to a query it isn't in the query in the same order as v1.

consider creating a PR on Playground

dulumao commented 4 years ago

Hope to develop a cache system plug-in

MilesLin commented 4 years ago

could remove cgo depend for sqlites

This is not GORM stuff. cgo come from the sql driver.

Here is the all driver list, some of them are using cgo. Unfortunately, sqlite is using cgo. https://github.com/golang/go/wiki/SQLDrivers

Hi-Rui commented 4 years ago

@jinzhu gorm.io/driver/mysql v0.3.1 gorm.io/gorm v0.2.31 Error 1292: Incorrect datetime value: '0000-00-00' for column 'created_at' at row 1 gorm.Model.CreatedAt default value db.Save(&row)

douyacun commented 3 years ago

hello world