Go gorm creating many to many relationship using customised table and extra data - go

I am trying to achieve a join between two models using gorm and a customised table. On this customised table I would like to store extra information about the joined models.
As per the docs I have the following,
func init() {
database.DB.SetupJoinTable(&Foo{}, "Bar", &FooBar{})
}
type Foo struct {
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;primaryKey;"`
SomeValue string `json:"someValue"`
Bars []Bar `json:"bars" gorm:"many2many:foo_bars"`
}
type Bar struct {
UUID uuid.UUID `json:"uuid" gorm:"type:uuid;primaryKey;"`
SomeValue string `json:"someValue"`
}
type FooBar struct {
FooUUID uuid.UUID `json:"foo" gorm:"primaryKey"`
BarUUID uuid.UUID `json:"bar" gorm:"primaryKey"`
ExtraValue string `json:"extraValue"`
}
The above creates a schema as I would expect. The problem is when trying to persist this relationship and set extra data needed to FooBar. The docs provide documentation on appending relationships directly from Foo to Bar, but not adding data to this customised table.
The documentation states:
JoinTable can be a full-featured model
So I would expect this to be possible.
Is this possible using gorm?
How can I create and save this relationship with additional information?

This is actually very simple, the fact that FooBar is a full-featured model means you can use it to interact with the join table directly the same way that any other model can:
// assuming we have a Foo foo and Bar bar
// read
links := []FooBar{}
db.Where(FooBar{FooUUID: foo.UUID, BarUUID: bar.UUID}).Find(&links)
// create
link := FooBar{FooUUID: foo.UUID, BarUUID: bar.UUID, ExtraValue: "foobar"}
db.Create(&link)
// update
link.ExtraValue = "foobarbaz"
db.Save(&link)
// delete
db.Delete(&link)
But to do all this requires you to treat the join table as a separate model. For example you shouldn't expect to be able to extract the ExtraValue when doing regular Associations operations on Foo, because there is literally no space on the []Bar field to put the value. Similar goes for saving the ExtraValue, unless you do some special trickery with Hooks (see this answer for more on this).

Related

How to prevent Child models from Deletion in Golang GORM?

Well, I would like to know, Is there any solutions, how to prevent Child Model from deletion in foreignKey Constraint, (
For example in gorm there is a couple of options that allows to restrict behavior of the Parent Model after deletion, and Delete or Set to Null the Child Model Objects That has foreignKey relation (onDelete: Cascade / Set Null, and the same thing for onUpdate)
// Pretty a lot of the same words, but I hope you got it :)
Little Example.. from Golang ...
type SomeOtherStruct struct {
gorm.Model
Id int
}
type SomeModel struct {
gorm.Model
someOtherStructId string
someField SomeOtherStruct `gorm:"foreignKey:SomeOtherStructId; OnDelete:Cascade,OnUpdate: SET NULL"` // basically foreign Key Relationship to model `SomeOtherStruct`
}
But I would like to prevent any Update/Deletion behavior, so Child Relation Models Objects won't get deleted after the Parent Model Object has been..
There is actually a concept from Django Framework (Python)
class SomeModel(models.Model):
some_field = models.ForeignKey(to=AnotherModel, verbose_name="SomeField", on_delete=models.PROTECT)
class AnotherModel(models.Model):
pass
As you can see, there is models.PROTECT constraint, that is basically what I'm looking for....
Is there any analogy for that in Golang GORM or some RAW SQL for that as well?
Thanks..
Unfortunately, you didn't mention which database you are using.
In Postgres (as an example) there are multiple options for ON DELETE:
NO ACTION
RESTRICT
CASCADE
SET NULL
SET DEFAULT
Only CASCADE will delete children if the parent is deleted. All other options (including the default NO ACTION) will make sure the children will "survive".
You can find more information in the postgres documentation: https://www.postgresql.org/docs/current/sql-createtable.html
Please feel free to update your question and/or comment with the database you are using.

How to permanently delete associations in GORM

I want to know how to permanently delete associations in GORM. I tried all examples shown in the documentation but I cannot get associations to become permanently deleted. For example, I am confused by GORM's documentation on deleting and clearing associations, which explicitly says: won't delete those objects from DB. (I don't understand what it means to delete objects without deleting them from the database.)
I have similar structs:
type User struct {
gorm.Model
City string `sql:"type:varchar(255);not null"`
Cards []Card `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
}
type Card struct {
ID uint `gorm:"primary_key"`
UserID uint `gorm:"column:user_id"`
}
I want to execute the following SQL query in GORM form:
DELETE c
FROM cards c
JOIN users u ON c.user_id = u.id
WHERE u.name = `Madrid`
gorm.Model is including a DeletedAt field. So on deletion, this will be set to the current date, the record won't be removed from the database, but will not be findable with normal query methods. They call that "soft delete".
In order to delete the record permanently you have to use Unscoped, like:
db.Unscoped().Delete(&order)
Source: https://gorm.io/docs/delete.html

"Creation At" time in GORM Customise Join table

I am trying to customize many2many table join. I have two tables from which I want to have taken the ids and want another field, which will tell me when the entry in the join table was made. The ids are coming fine, but the "created_at" is not updating and shows "Null" instead of time.
// this is the table join struct which I want to make
type UserChallenges struct {
gorm.JoinTableHandler
CreatedAt time.Time
UserID int
ChallengeID int
}
//hook before create
func (UserChallenges) BeforeCreate(Db \*gorm.DB) error {
Db.SetJoinTableHandler(&User{}, "ChallengeId", &UserChallenges{})
return nil
}
This is not giving any error on the build. Please tell me what I am missing so that I can get the creation time field in this.
PS - The documentation of GORM on gorm.io is still showing SetupJoinTable method but it is deprecated in the newer version. There is a SetJoinTableHandler but there is no documentation available for it anywhere.
The thing to get about using a Join Table model is that if you want to access fields inside the model, you must query it explicitly.
That is using db.Model(&User{ID: 1}).Association("Challenges").Find(&challenges) or db.Preload("Challenges").Find(&users), etc. will just give you collections of the associated struct and in those there is no place in which to put the extra fields!
For that you would do:
joins := []UserChallenges{}
db.Where("user_id = ?", user.ID).Find(&joins)
// now joins contains all the records in the join table pertaining to user.ID,
// you can access joins[i].CreatedAt for example.
If you wanted also to retrieve the Challenges with that, you could modify your join struct to integrate the BelongsTo relation that it has with Challenge and preload it:
type UserChallenges struct {
UserID int `gorm:"primaryKey"`
ChallengeID int `gorm:"primaryKey"`
Challenge Challenge
CreatedAt time.Time
}
joins := []UserChallenges{}
db.Where("user_id = ?", user.ID).Joins("Challenge").Find(&joins)
// now joins[i].Challenge is populated

Gorm (Golang) and database with Single Table Inheritance "type" column

I'm experimenting with using Go to read from a database that's been part of an existing Rails app. A few of the models and therefore tables use single table inheritance via a type column. In Rails/ActiveRecord, the presence of this column will create an automatic mapping to the appropriate model. If the table is animals and type is Dog, it will map to the Dog class; if it's Cat, it'll map to the Cat class. I want to setup something similar in Gorm.
Since it doesn't seem like Gorm has a default_scope option for a model, I'm using a new callback.
func scopedSearch(scope *gorm.Scope) {
tablename := scope.TableName()
switch tablename {
case "table_using_sti":
scope.Search.Where("type = ?", "MyModelName")
default:
return
}
}
And then I register the callback in my main function:
db.Callback().Query().Before("gorm:query").Register("my_plugin:before_query", scopedSearch)
When I search against an instance using db.First or the model using db.Model(&MyModel{}).Where(...), it appears to be working. Is this the right way to handle it? Will this scope be respected by all of the query methods or is there something more direct or thorough?

Gorm creates duplicate in association

I have the following 2 structs with a many-2-many relationship.
type Message struct {
gorm.Model
Body string `tag:"body" schema:"body"`
Locations []Location `tag:"locations" gorm:"many2many:message_locations;"`
TimeSent time.Time `tag:"timesent"`
TimeReceived time.Time `tag:"timereceived"`
User User
}
type Location struct {
gorm.Model
PlaceID string `tag:"loc_id" gorm:"unique"`
Lat float64 `tag:"loc_lat"`
Lng float64 `tag:"loc_lng"`
}
If I create a Message with DB.Create(my_message), everything works fine : the Message is created in DB, along with a Location and the join message_locations table is filled with the respective IDs of the Message and the Location.
What I was expecting though was that if the Location already exists in DB (based on the place_id field, which is passed on), gorm would create the Message, retrieve the Location ID and populate message_locations.That's not what is happening.
Since the PlaceID must be unique, gorm finds that a duplicate key value violates unique constraint "locations_place_id_key" and aborts the transaction.
If on the other hand I make the PlaceID not unique, gorm creates the message alright, with the association, but then that creates another, duplicate entry for the Location.
I can test if the location already exists before trying to save the message:
existsLoc := Location{}
DB.Where("place_id = ?", mssg.Locations[0].PlaceID).First(&existsLoc)
then if true switch the association off:
DB.Set("gorm:save_associations", false).Create(mssg)
DB.Create(mssg)
The message is saved without gorm complaining, but then message_locations is not filled.
I could fill it "manually" since I've retrieved the Location ID when testing for its existence, but it seems to me it kind of defeats the purpose of using gorm in the first place.
I'm not sure what the right way to proceed might be. I might be missing something obvious, I suspect maybe something's wrong with the way I declared my structs? Hints welcome.
UPDATE 2016/03/25
I ended up doing the following, which I'm pretty sure is not optimal. If you have a better idea, please chime in.
After testing if the location already exists and it does:
// in a transaction
tx := DB.Begin()
// create the message with transaction disabled
if errMssgCreate := tx.Set("gorm:save_associations", false).Create(mssg).Error; errMssgCreate != nil {
tx.Rollback()
log.Println(errMssgCreate)
}
// then create the association with existing location
if errMssgLocCreate := tx.Model(&mssg).Association("Locations").Replace(&existLoc).Error; errMssgLocCreate != nil {
tx.Rollback()
log.Println(errMssgLocCreate)
}
tx.Commit()
In my situation I was using a UUID for the ID. I coded a BeforeCreate hook to generate the uuid. When saving a new association, there was no need for the beforeCreate hook to create a new ID, but it did so anyway (that feels a bit like it could be a bug?).
Note that it did this even when using "association" mode to append a new relationship. The behaviour was not limited to when calling Create with a nested association.
It took me several hours to debug this because when I inspected the contents of the associated records they matched exactly the instances that I had just created.
In other words:
I made a bunch of Foo
I made some Bar, and tried to attach just certain Foo to each
The Foo in the Bar relationship had the same reference as the objects I had just created.
Removing the beforeCreate hook makes the code behave like I'd expect. And happily I was already in the habit of manually making a uuid whenever needed instead of relying on it so it didn't hurt me to remove it.
I've pasted a minimally reproducible example at https://pastebin.com/wV4h38Qz
package models
import (
"github.com/google/uuid"
"gorm.io/gorm"
"time"
)
type Model struct {
ID uuid.UUID `gorm:"type:char(36);primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}
// BeforeCreate will set a UUID rather than numeric ID.
func (m *Model) BeforeCreate(tx *gorm.DB) (err error) {
m.ID = uuid.New()
return
}
gorm:"many2many:message_locations;save_association:false"
Is getting closer to what you would like to have. You need to place it in your struct definition for Message. The field is then assumed to exist in the db and only the associations table will be populated with data.

Resources