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

Provide scanonly or insertonly #374

Closed
bgdnxt opened this issue Dec 21, 2021 · 11 comments
Closed

Provide scanonly or insertonly #374

bgdnxt opened this issue Dec 21, 2021 · 11 comments
Labels
enhancement New feature or request stale

Comments

@bgdnxt
Copy link
Contributor

bgdnxt commented Dec 21, 2021

I have a generated column in PG table, and applied scanonly tag in model, here is the example code:

# the schema
CREATE TABLE IF NOT EXISTS users (
    id bigint PRIMARY KEY DEFAULT snow_gen_id(),
    created_at timestamptz DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamptz DEFAULT CURRENT_TIMESTAMP,
    object_id varchar(128) GENERATED ALWAYS AS ('USR' || cast(id as varchar)) STORED
}

# the model
type User struct {
	ID        int64     `json:"id,string"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
	ObjectID  string    `json:"objectId" bun:",scanonly"`
}

# the selection

user := &User{}
err := bundb.NewSelect().Model(user).Where("id = ?", id).Scan(context.background())

# the debug print sql

SELECT "user"."id", "user"."created_at", "user"."updated_at" FROM "users" AS "user" ORDER BY "id" ASC

I would expect the object_id appear in the query, but it doesn't.

@vmihailenco
Copy link
Member

scanonly is for scanning results, for example, SELECT t.a + t.b AS sum. It can be added without complicating the code so that's why we have it.

For now, you can split your model like this:

type UserData struct {
	bun.BaseModel `bun:"users"`

	ID        int64     `json:"id,string"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

type User struct {
	UserData `bun:",inherit"`
	ObjectID  string    `json:"objectId"`
}

Not sure if we need selectonly or insertonly to support this more elegantly. Time will tell.

@vmihailenco vmihailenco added the enhancement New feature or request label Dec 21, 2021
@bgdnxt
Copy link
Contributor Author

bgdnxt commented Dec 22, 2021

Got it, so I'm misleading for the scanonly. looking forward for the enhancement.

@vmihailenco vmihailenco changed the title scanonly tag not work on select Provide scanonly or insertonly Feb 22, 2022
@imraan-go
Copy link

inherit

What does this "inherit" do? It is not documented anywhere!

@vmihailenco
Copy link
Member

That's an alias for ,extend - I've documented it here.

@imraan-go
Copy link

@vmihailenco Can we have a readonly and insertonly tag? readonly will be so helpful.

@vmihailenco vmihailenco reopened this Mar 23, 2022
@vmihailenco
Copy link
Member

I did not mean to close this.

@imraan-go could you describe your use case so I have an idea how this will be used?

@imraan-go
Copy link

"id" uuid NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY

@vmihailenco for this type of schenario readonly will be useful and clear. I want database to autogenerate the uuid. Currently the way to implement this is to use bun tag autoincrement to insert default in the sql query. But the autoincrement tag is really confusing in this case and feels like a hack. readonly tag will make it more clear that the column will be ommitted in insert or update. readonly will behave more like a ExcludeColumn on table level instead of query level.

@vmihailenco
Copy link
Member

@imraan-go it should be nullzero not autoincrement

type Model struct {
    ID UUD `bun:",pk,nullzero"`
}

@codeliger
Copy link
Contributor

How can we select computed columns without insert/updating them? There seems to be no solution currently.

@dionb
Copy link

dionb commented Sep 26, 2023

My immediate usecase for insertonly would be for CreatedAt and CreatedBy fields. To ensure that they are only written to on creation of a resource, and to protect against devs making mistakes.

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 20, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stale
Projects
None yet
Development

No branches or pull requests

5 participants