uptrace / bun

SQL-first Golang ORM
https://bun.uptrace.dev
BSD 2-Clause "Simplified" License
3.67k stars 227 forks source link

Is is possible with Bun to insert with relationships? #15

Open frederikhors opened 3 years ago

frederikhors commented 3 years ago

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.

vmihailenco commented 3 years ago

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 :)

frederikhors commented 3 years ago

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!

vmihailenco commented 3 years ago

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 :)

frederikhors commented 3 years ago

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?

vmihailenco commented 3 years ago

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.

GamerGirlandCo commented 1 year ago

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?