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

inconsistent behavior for ModelTableExpr (select <--> create/insert/update/delete) #968

Open
mingoal opened this issue Mar 27, 2024 · 0 comments · May be fixed by #975
Open

inconsistent behavior for ModelTableExpr (select <--> create/insert/update/delete) #968

mingoal opened this issue Mar 27, 2024 · 0 comments · May be fixed by #975

Comments

@mingoal
Copy link

mingoal commented Mar 27, 2024

bun@v1.1.17

bun allows using ModelTableExpr to rename table, insert/update could operate on renamed table, but query_select doesn't support to select from it.

I raised #965 but it was closed.

From the tracing log, select ignored the parameter of ModelTableExpr when constructing the sql statement.

[bun]  08:16:07.856   CREATE TABLE          1.421ms  CREATE TABLE renamed_table_name ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR)
[bun]  08:16:07.856   INSERT                  168µs  INSERT INTO renamed_table_name ("name") VALUES ('admin') RETURNING "id"
[bun]  08:16:07.856   UPDATE                   86µs  UPDATE renamed_table_name SET "name" = 'root' WHERE (id = 1)
[bun]  08:16:07.857   SELECT                   32µs  SELECT "user"."id", "user"."name" FROM renamed_table_name WHERE (id = 1)     *sqlite.Error: SQL logic error: no such column: user.id (1) 
Fail to select user  SQL logic error: no such column: user.id (1)
[bun]  08:16:07.857   DELETE                   80µs  DELETE FROM renamed_table_name WHERE (id = 1 )
func DemoBunOperation() {
	sqldb, err := sql.Open(sqliteshim.ShimName, "file::memory:?cache=shared")
	if err != nil {
		panic(err)
	}

	db := bun.NewDB(sqldb, sqlitedialect.New())

	db.AddQueryHook(bundebug.NewQueryHook(
		bundebug.WithVerbose(true),
		bundebug.FromEnv("BUNDEBUG"),
	))

	ctx := context.Background()
	type User struct {
		bun.BaseModel

		ID   int64 `bun:",pk,autoincrement"`
		Name string
	}

	renamedTable := "renamed_table_name"
	db.NewCreateTable().Model((*User)(nil)).ModelTableExpr(renamedTable).Exec(ctx)
	user := &User{Name: "admin"}
	db.NewInsert().Model(user).ModelTableExpr(renamedTable).Exec(ctx)
	user.Name = "root"
	_, err = db.NewUpdate().Model(user).ModelTableExpr(renamedTable).Where("id = ?", user.ID).Exec(ctx)
	if err != nil {
		fmt.Println("Fail to update user ", err.Error())
	}
	readUser := new(User)
	err = db.NewSelect().Model(readUser).ModelTableExpr(renamedTable).Where("id = ?", user.ID).Scan(ctx)
	if err != nil {
		fmt.Println("Fail to select user ", err.Error())
	}
	_, err = db.NewDelete().Model(user).ModelTableExpr(renamedTable).Where("id = ? ", user.ID).Exec(ctx)
	if err != nil {
		fmt.Println("Fail to delete user ", err.Error())
	}
}
@mingoal mingoal changed the title inconsistent behavior for ModelTableExpr inconsistent behavior for ModelTableExpr (select <--> create/insert/update/delete) Mar 27, 2024
@JunNishimura JunNishimura linked a pull request Apr 7, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant