#Gorm - CreatedBy and UpdatedBy logic within models
23 messages · Page 1 of 1 (latest)
above is my company, which i would expect i could return user details whose created / updated this company object
type User struct {
ID uuid.UUID `gorm:"index:id,unique;type:uuid;default:gen_random_uuid();" json:"id"`
Name string `json:"name" gorm:"not null" binding:"required"`
Address string `json:"address,omitempty"`
Username string `json:"username" binding:"required" gorm:"not null"`
Email string `gorm:"email:id,unique" json:"email" binding:"required" gorm:"not null"`
Password string `json:"password" binding:"required" gorm:"not null"`
Role string `json:"role,omitempty" binding:"required" gorm:"not null"`
CreatedBy uuid.UUID `json:"createdBy" gorm:"type:uuid;default:NULL"`
UpdatedBy uuid.UUID `json:"updatedBy" gorm:"type:uuid;default:NULL"`
CreatedBySuperAdmin []*User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:CreatedBy;references:ID" json:"createdBySuperAdmin"`
UpdatedBySuperAdmin []*User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UpdatedBy;references:ID" json:"updatedBySuperAdmin"`
CompanyCreatedByUser []Company `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:CreatedBy;references:ID" json:"companyCreatedByUser"`
CompanyUpdatedByUser []Company `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UpdatedBy;references:ID" json:"companyUpdatedByUser"`
//ClientCreatedByUser *Client `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:CreatedBy;references:ID" json:"clientCreatedByUser"`
//ClientUpdatedByUser *Client `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UpdatedBy;references:ID" json:"clientUpdatedByUser"`
types.DefaultModelProperty
}
and here is my user model, which i would expect when i fetch this user i can see the companies that i created within this user
or here's the screenshot for the user model
the relation of company inside user model
here i tried to fetch the single user within a company object
but it gives me nil on the return for the User relation in company model
but within the database editor, they're both interconnected between each other
but gorm doesnt seem to understand this logic, or i might be wrong
i've also readed the gorm's documentation about one-to-many or has many in this case
Has ManyA has many association sets up a one-to-many connection with another model, unlike has one, the owner could have zero or many instances of models. For example, if your application includes use
but in their documentation, how does i retrieve the user whose created the credit card ? doesn't really explaining how
thats because gorm is a wrapper for SQL relational databases
what you are doing here is a pretty common whoopsie oopsie for beginners
you either use embedded structs or (better) use a proper relation that faces your needs
okay so ive replied too fast uve already found that documentation, see; gorm retrieves the Users, then it looks for foreign key constrains (for example the UserID uint in the CreditCard), it automatically detects those models
lemme show you an example;
type User struct {
Id uuid.UUID `json:"id" gorm:"type:char(36);primary_key"`
CartID uuid.UUID `json:"cart_id" gorm:"foreignkey:UserID"`
Cart Cart `json:"cart" gorm:"association_foreignkey:ID;foreignkey:CartID"`
}```
**CartID**: This field serves as the foreign key in the User model, linking it to the Cart model, it indicates which Cart is associated with a particular user
**Cart**: This field is a direct association to the Cart model, it is used to load the associated Cart when querying for a user. It's essentially a convenience field that allows you to access the associated Cart directly from a User instance
however if you only need the CartID for foreign key relationships and don't often need to access the full Cart data when working with a User, you should consider omitting the Cart field and fetching the Cart separately when needed, especially if working with sensitive data models or passwords as they can be easily ( & mistakenly ) shown where they shouldnt
the Cart model is also pretty simple:
type Cart struct {
Id uuid.UUID `json:"id" gorm:"type:char(36);primary_key"`
UserId uuid.UUID `json:"user_id" gorm:"type:char(36);column:user_id"`
}```
The UserId here has the same usage as the CartId as described above