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

Support for readonly transient field #394 #398

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wingsofovnia
Copy link

@wingsofovnia wingsofovnia commented Jun 17, 2019

The issue #394 raises a very typical use case for fields that are not physically present in the table as a column, however, they are computed on reads.

For example, if I want to have a full_name property populated in type User struct { f_name string, s_name string, full_name string} for a table User = { f_name, s_name } the query

dbMap.SelectOne(&user, 
"select f_name, s_name, concat(f_name, s_name) full_name" from user...)

will successfully map the result as expected, however, the inserts will fail.

If I ignore full_name field via db:"-", inserts will work as expected, but reads will raise NoFieldInTypeError. Despite this is a "non-fatal error", it breaks the old codebase that expects no errors.

This change introduces a new field Tag "readonly" that behaves exactly as a transient field (db.go#readStructColumns maps such column to ColumnMap with Transient=true) , except gorp.go#columnToFieldIndex won't skip it for mapping select results.

Tested locally with postgres db.

@nelsam @hinet

@wingsofovnia
Copy link
Author

CI fails on TestNullTime test that is not related to the change

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 this pull request may close these issues.

1 participant