Describe the feature

Consider these models:

type BaseModel struct {
  ID        uint `gorm:"primaryKey"`
  CreatedAt *time.Time
  UpdatedAt *time.Time
}

type User struct {
  BaseModel
  Name      string
  CompanyID uint
  Company   Company
}

type Company struct {
  BaseModel
  Name string
}

In a query joining User and Company models, this feature would allow one to Select() or Omit() fields shared between these two models with a wildcard like this:

someUserID := 1337
user := User{}
DB.
  Joins("Company").
  Omit("*.CreatedAt", "*.UpdatedAt").
  Take(&user, someUserID)

Expected result:

User: {
  ID: 1337,
  CreatedAt: nil,
  UpdatedAt: nil,
  Name: "foo",
  Company: {
    ID: 42,
    CreatedAt: nil,
    UpdatedAt: nil,
    Name: "bar",
  },
}

Motivation

Currently, you have to specify Select or Omit for every join/preload you do in a query. If we go back to the example we saw earlier, to obtain the same results, you'll need to proceed as follows:

someUserID := 1337
user := User{}
DB.
  Joins("Company", DB.Omit("CreatedAt", "UpdatedAt")).
  Omit("CreatedAt", "UpdatedAt").
  Take(&user, someUserID)

Whilst this example doesn't look too bad in such a simple request, it can certainly get nasty as you get more than one Joins/Preload in your request.

It's quite common to have a BaseModel shared across all models in an app, and I believe more than one might find it useful to have such a feature. I'll see if I can manage to implement this, from what I've seen it might not be too much of a change.

Related Issues

N/A

Comment From: jinzhu

Thank you for your request, but not going to implement this one as it might not be that valuable compared to its complexity, maybe you can use Scopes to remove duplicated codes