Any chance to add ability for custom fieldColumnProperty
to interface which is used for select?
#341
wangxin688
started this conversation in
Ideas
Replies: 1 comment
-
SqlAlchemy is an ORM and Jet is an SQLBuilder, so it is not possible to have such a feature built in as part of library. But you can extend jet models to achieve the same result. For instance you can wrap autogenerated model type User struct {
model.User
}
func (u User) GetAddressCount() (int, error){
stmt := SELECT(COUNT(STAR)).
FROM(Address).
WHERE(Address.UserID.EQ(u.User.ID))
var dest int
err := stmt.Query(db, &dest)
//wrap err
return dest, err
} To have complete ORM like experience you can also add a method to load user by id: func (u *User) Load(id int64) error {
stmt := SELECT(User.AllColumns).
FROM(User).
WHERE(User.ID.EQ(Int(id)))
return stmt.Query(db, u)
} Now you can write: var user User
err := user.Load(1234)
addressCount, err := user.GetAddressCount() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
it's powerful to use
column_property
introduce in sqlalchemy for subquery some custom field.eg: to query related object count is really easy to use.
Beta Was this translation helpful? Give feedback.
All reactions