Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insertion without a model #739

Open
lexkrstn opened this issue Jan 1, 2023 · 8 comments
Open

Insertion without a model #739

lexkrstn opened this issue Jan 1, 2023 · 8 comments
Labels

Comments

@lexkrstn
Copy link

lexkrstn commented Jan 1, 2023

How to use bun as a query builder to insert a row into the database?

When I try this:

_, err = r.db.NewInsert().
	Table("custom_path").
	Value("port_a_id", portAId).
	Value("port_b_id", portBId).
	Value("checkpoints", strings.Join(checkpoints, ",")).
	Value("path", "st_geomfromgeojson(?)", pathJson).
	On("conflict (port_a_id, port_b_id, checkpoints) do update").
	Set("path = excluded.path").
	Exec(r.ctx)

I get an error "bun: Model(nil)".

@vmihailenco vmihailenco added the bug Something isn't working label Jan 5, 2023
@vmihailenco
Copy link
Member

Looks like a bug.

@vmihailenco vmihailenco added the good first issue Good for newcomers label Jan 5, 2023
@moutoum
Copy link

moutoum commented Aug 8, 2023

Hi, I'd be very interested by this feature aswell. Do you have a plan to implement this?
Thanks

@i-fix-retarded-bugs
Copy link

Still not fixed? Well

Copy link

github-actions bot commented Nov 7, 2024

This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed.

@github-actions github-actions bot added the stale label Nov 7, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2024
@your-diary
Copy link

Any updates?

@j2gg0s j2gg0s reopened this Feb 27, 2025
@j2gg0s j2gg0s added no stalebot and removed stale labels Feb 27, 2025
@j2gg0s
Copy link
Collaborator

j2gg0s commented Mar 6, 2025

_, err = r.db.NewInsert().
Table("custom_path").
Value("port_a_id", portAId).
Value("port_b_id", portBId).
Value("checkpoints", strings.Join(checkpoints, ",")).
Value("path", "st_geomfromgeojson(?)", pathJson).
On("conflict (port_a_id, port_b_id, checkpoints) do update").
Set("path = excluded.path").
Exec(r.ctx)

Why not use raw query?

db.NewRaw("INSERT INTO custom_path(port_a_id, port_b_id) VALUES(?, ?)", portAId, portBId).Exec(ctx)

@j2gg0s j2gg0s removed the bug Something isn't working label Mar 6, 2025
@your-diary
Copy link

your-diary commented Mar 6, 2025

@j2gg0s

Why not use raw query?

I agree the example in the OP may be a bit too simplified, but in real-world situations, I believe ORM is often utilized to dynamically (conditionally) construct SQL queries:

query := db.NewInsert().Table("some_table")
if someCondition {
    query = query.Value(...)
}
if anotherCondition {
    query = query.Value(...)
}
query.Exec(...)

So, while NewRaw() is useful, I think it does not solve the core point of this issue.

@j2gg0s
Copy link
Collaborator

j2gg0s commented Mar 6, 2025

From the example, it seems more like a SQL Builder rather than an ORM.

On the other hand, the core of Bun is the Model. There is no way to generate SQL without relying on the Model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants