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

Create temp table from select query #949

Open
erstam opened this issue Jan 29, 2024 · 4 comments
Open

Create temp table from select query #949

erstam opened this issue Jan 29, 2024 · 4 comments
Assignees
Labels

Comments

@erstam
Copy link

erstam commented Jan 29, 2024

Using bun 1.1.16 here.

I am trying to use (bun.IDB).NewCreateTable().Temp().IfNotExists().ModelTableExpr("my_temp_table_name AS (?)", selectQuery).Exec(ctx)

(whatever is selectQuery, it is just a query using SELECT ...)

I would expect this call to render this query: CREATE TEMP TABLE IF NOT EXISTS my_temp_table_name AS (selectQuery) which at least on Postgres is supported: https://www.postgresql.org/docs/current/sql-createtable.html

Instead of rendering the query, the code panics: invalid memory address or nil pointer dereference

panic({0x22657e0, 0x2140aa0})
        src/runtime/panic.go:890 +0x262
github.com/uptrace/bun.(*CreateTableQuery).beforeCreateTableHook(0xc000405040, {0x28e94d8, 0xc000718050})
        github.com/uptrace/[email protected]/query_table_create.go:351 +0x58
github.com/uptrace/bun.(*CreateTableQuery).Exec(0xc000405040, {0x28e94d8, 0xc000718050}, {0x0, 0x0, 0x0})
        github.com/uptrace/[email protected]/query_table_create.go:325 +0x85
func (q *CreateTableQuery) beforeCreateTableHook(ctx context.Context) error {
	if hook, ok := q.table.ZeroIface.(BeforeCreateTableHook); ok {    // panic on this line because ZeroIface is nil
		if err := hook.BeforeCreateTable(ctx, q); err != nil {
			return err
		}
	}
	return nil
}

It panic because, I think, I am not specifying a "model", by calling the Model() function. However, when I do this, giving a "dummy" struct with zero field as the model, I instead get the following query:

CREATE TEMP TABLE IF NOT EXISTS my_temp_table_name AS (selectQuery) ()

Notice the empty () at the end of the query. It causes a syntax error on SQL.

Any idea on how I could solve this ? (besides executing a raw query if possible)

Copy link

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 17, 2024
@erstam
Copy link
Author

erstam commented Nov 19, 2024

ping, will this gets resolved instead of closed by a bot?

@j2gg0s j2gg0s removed the stale label Nov 20, 2024
@j2gg0s j2gg0s self-assigned this Nov 20, 2024
@j2gg0s
Copy link
Collaborator

j2gg0s commented Nov 20, 2024

I'm sorry that BUN does not support the CREATE ... AS SELECT syntax.

ModelTableExpr only change table's name

Copy link

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 Dec 21, 2024
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

2 participants