-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GORM v2 is on going #2886
Comments
@jinzhu Could you please add support for generation of migration files like djangos makemigrations? Django's orm is pretty stable, very useful and has lot of good features. Ref: https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-makemigrations Example Django application with migrations: https://github.com/stadtulm/cykel/tree/master/bikesharing/migrations |
|
Preloads using joins for PostgreSQL (I don't know how that's implemented in other dialects), for me that is by far the most important feature absent in the current version. As it currently stands, preloads literally stop working once the number of parameters exceed a certain amount. Before then preloads are prohibitively slow partly due to the extra roundtrip per preload, but more importantly due to the missed opportunity for query optimization. |
Has any work on static code generator started? I'd be interested in contributing. |
Hi @rjeczalik haven't started it, just created some draft notes, was going to start it after the release of v2, but if you are interested, I can create a project for the generator under GORM's organization. ( yes, we will have an organization ;) ) Here is the draft notes (feedback is welcome) Generate static code based on relationships parsed by https://github.com/jinzhu/gorm/tree/v2_dev/schemagenerated code looks like package models // user defiend
type DB struct {
DB *gorm.DB
User User
}
type User struct {
ID IntField
Name StringField
Languages HasManyRelation
}
var user = User{
ID: IntField{
// xxx
},
Name: StringField{
// xxx
}
Languages: HasManyRelation{
// xxx
}
}
func New(gorm.Dialector, gorm.Config) *DB {
return &DB{
DB: gormDB,
User: user,
}
} Usage APIinitalize dbdb := models.NewDB(sqlite.Open('xxx'), gorm.Config{
}) with contextdb = db.WithContext(ctx) API// find
db.User.Select(db.User.Name, db.User.Age).Find() (users []*yourapp.User, err error)
// first
db.User.Select(db.User.Name, db.User.Age).First() (*yourapp.User, error)
// last
db.User.Select(db.User.Name, db.User.Age).Last() (*yourapp.User, error)
// last
db.User.Where(db.User.Name.Like("%jinzhu"), db.User.Age.Eq(10)).Last() (*yourapp.User, error)
// preload
db.User.Preload(db.User.Languages.Where(
db.Languages.Code.Eq("zh-CN")
)).Select(db.User.Name, db.User.Age).Last() (*yourapp.User, error)
// update
db.User.Update(user)
// Relations
db.User.Languages.Model(user).Add([]*yourapp.Languages{})
db.User.Languages.Model(user).Replace(]*yourapp.Languages{})
db.User.Languages.Model(user).Delete([]*yourapp.Languages{}) |
@jinzhu Not quite sure what e.g. There's already a functional generator for MySQL - https://github.com/smallnest/gen. What I wanted to achieve is db-agnostic generator with the ability of generating fields for associations (preloading), configurable via struct tags. |
Maybe instead of coming up with another migration toolkit, have documentation and examples (or an integration) for using pressly/goose for migrations. There are a few other libraries, but goose can be fully integrated or it can be standalone with sql files for the migrations. Couple that with a generator that follows a convention or specified template for the migrations (generator like in rails, where you generate a new migration file with a rails command) /cc @donutloop |
@rjeczalik sorry, haven't made things clear, this tool WON'T generate models definition from database You need to defined it by yourself, for example, you already have your user package package user
type User struct {
gorm.Model
Name string
Language Language
LanguageCode string
}
type Language struct {
Code string `gorm:primarykey`
} With this tool, it will generate another package named package db
type DB struct {
DB *gorm.DB
User User
}
type User struct {
ID IntField
Name StringField
Languages HasManyRelation
}
func (user User) First() (user.User, error) {
// xxx
}
var user = User{
ID: IntField{
DBName: "id",
},
Name: StringField{
DBName: "name",
}
Languages: HasManyRelation{
ReferenceTable: "languages",
ForeignKey: "code",
}
}
func New(gorm.Dialector, gorm.Config) *DB {
return &DB{
DB: gormDB,
User: user,
}
} In your application, if you want to use the generated code like: import 'yourapp/db'
var db = db.New(gorm.dialector, gorm.Config{})
func handler(w http.ResponseWriter, r *http.Request) {
user, err := db.User.First()
fmt.Println(user.Name) // string
json.NewEncoder(w).Encode(user)
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
} |
Please review some crucial fixes, we even had to use our fork to mitigate these problems: https://github.com/jinzhu/gorm/pull/2737 -- erros on nested preloads get ignored, this is a critical issue. Also please support context for cancellation and tracing support. Our fork with context support and fix for ignored nested preload errors: https://github.com/readdle/gorm/tree/nested-preload-fix |
@jinzhu Let me know if I understand it correctly - you want to have a generator which will generate objects with API similar to what Active Record in RoR is offering, correct? |
@rjeczalik yes, generate type-safe code with similar API like that, suggestions? |
allow https://golang.org/pkg/context/ to be passed to the underlying SQL driver. |
@edwardsb the API will work like this: tx := db.WithContext(ctx)
tx.First(&user)
tx.Update(&user) db.WithContext(ctx).First(&user) (similar to https://golang.org/pkg/net/http/#Request.WithContext) |
could remove cgo depend for sqlites |
dynamic model support |
@MurtadhaS just finished that ;) |
The most important feature for my team would be a more advanced migration tool. I'm really missing Django's migration framework. Integration into Goose would be fine, as long as the up/down SQL is generated by Gorm. The main thing is that the migration tool is able to determine automatically what has changed between versions. |
Postgres ErrorCodes support please. |
Savepoint (nested transaction) support in transaction please. |
Also better handling of Postgres |
Another thing. |
Hi. Please check out my PR #2921 |
Have a clear-well-defined API. In my experience with gorm I found that there are multiple ways to achieve the same things which bothered me a lot. I could provide some examples in the next day or so |
@vzool will make it works with API like |
There is no api for subqueries. |
@jinzhu will v2 work with qor admin? |
Hi,jinzhu, I Use |
QOR Admin will migrate to GORM v2, but I need to check with the current maintainer for its plan.
Working on it should be ready in the next two weeks
Please follow the document, if you believe there is an issue, create a PR here https://github.com/go-gorm/playground |
@jinzhu Hi I encountered an issue with the
Results in a panic
Without |
the value of |
Add a way for omitting some columns also on querying, not only on saving. |
I am setting below thing for each request in http middleware.
Can you please give the replacement for the above thing. @jinzhu @moul |
I have finished the draft v2 documents, please check it out http://v2.gorm.io (will use http://gorm.io after the final release) Closing this issue, please create a new issue on any bug report or suggestion. |
Tried out GORM v2 and it is fantastic. However, I am having difficulty finding a proper way of implementing migrations. Can we expect a migration for v2 similar to that of Ruby on Rails/Django @jinzhu ? The problem is that once I run the auto-migration I need to manually delete or update the names of the column of tables if it requires changes. |
@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. |
Question, I've been rewriting an API backend which utilized Gorm 1.X, and I notice the following doesn't return any associated When hitting the API with /series/1 (which is valid and created with the following CURL command: {
"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 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
} |
when i write a custom logger, how can i get |
@jinzhu great work, thanks! I'm currently trying out v2 (MySQL) and have 3 questions:
|
http://v2.gorm.io/docs/connecting_to_the_database.html#Connection-Pool |
initialize the logger with DB |
@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. |
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. |
can I use the last commit in production ? |
There are hundreds of services already using GORM V2 ;) |
|
consider creating a PR on Playground |
Hope to develop a cache system plug-in |
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. |
@jinzhu |
hello world |
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.
The text was updated successfully, but these errors were encountered: