bun icon indicating copy to clipboard operation
bun copied to clipboard

Is is possible with Bun to insert with relationships?

Open frederikhors opened this issue 3 years ago • 6 comments

Is it possible with Bun to insert a Team with it's has-many Players using a single insert?

Example:

team := Team{
  name: "Real"
  players: []Player{
    {name: "John"},
    {name: "Frank"}
  }
}

db.NewInsert().Model(Team).Exec(ctx)

Is this possible?

Something like https://github.com/go-pg/pg/issues/478.

frederikhors avatar Jun 04 '21 18:06 frederikhors

No, that is not supported. I will accept a PR that contributes something like that, but I personally don't find such feature useful so don't expect it from me :)

vmihailenco avatar Jun 06 '21 08:06 vmihailenco

I will accept a PR that contributes something like that

I wish I was able to do it!

I understand that you see this problem with this perspective, this is not a criticism.

I just want you to understand how maybe another person (me) sees it.

The convenience of writing a model composed of has-many or has-one and being able to save and update it automatically is priceless to me!

Something like:

type Player struct {
  Name string

  Games []Game
}

type Game struct {
  World string
  Score int
}

with this command

_, err := db.NewInsert().Model(player).Exec(ctx)

which generates one SQL statement both for Player and all it's Games. The same for UPDATE and DELETE.

This convenience is invaluable (especially if there are many tables related).

But I understand that it is not in your priorities and I RESPECT your opinion!

Maybe someone will help us sooner or later!

Thank you!

frederikhors avatar Jun 06 '21 11:06 frederikhors

Such feature requires you to structure API in a certain way. Specifically, you need to add Player.Games field and make sure to populate it properly (accept data in a certain way). That is the additional code you need to write in order to use cascading inserts.

But eventually you will want to customize the insert logic, for example, use insert-or-update. Or cache. Or add some validation. Bun can't support any of that.

So you will throw away cascading inserts and all the efforts you've made to support them. And replace it with 20-30 lines of code that just calls a couple of inserts. The last thing to do is to write a blog post / comment saying "ORMs are evil".

PS Another popular alternative is to fix the problem with hooks. I guess we will discuss that in hooks-related thread :)

vmihailenco avatar Jun 10 '21 10:06 vmihailenco

So you will throw away cascading inserts and all the efforts you've made to support them. And replace it with 20-30 lines of code that just calls a couple of inserts. The last thing to do is to write a blog post / comment saying "ORMs are evil".

LOLOLOLOLOLOLOLOL!!!!!!

You got me!

PS Another popular alternative is to fix the problem with hooks.

I'm curious, what do you mean?

frederikhors avatar Jun 10 '21 12:06 frederikhors

I'm curious, what do you mean?

You could try to implement insert-or-update/cache/validation using BeforeInsert/AfterInsert model hook. Assuming that hooks are powerful enough.

vmihailenco avatar Jun 11 '21 14:06 vmihailenco

You could try to implement insert-or-update/cache/validation using BeforeInsert/AfterInsert model hook. Assuming that hooks are powerful enough.

How would you access the bun.DB instance from within a hook function to create a new query though?

GamerGirlandCo avatar Sep 19 '23 18:09 GamerGirlandCo