Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Maran
@maran
uuh
Alexey Palazhchenko
@AlekSi
Wait :)
Maran
@maran
Hahah sorry
maran @maran sits there quietly
Alexey Palazhchenko
@AlekSi
Basically, reform does not change the way how driver works and maps values to columns and back
It just uses normal Scan() and Value() methods of database/sql and driver
BeforeInsert() and other hooks are for special cases like setting a time zone or truncating time to seconds, for example
Maran
@maran
I must still be making an error somewhere; if the tests pass so should my super minimal code example.
Alexey Palazhchenko
@AlekSi
One thing I see now is that created_at is NULLable, but CreatedAt fields is not a pointer
Maran
@maran
ah let me fix that
Alexey Palazhchenko
@AlekSi
mattn/go-sqlite3#190
Maran
@maran
Yeah noticed that as well.
2018/02/02 14:11:17 NOT NULL constraint failed: artists.created_at
So that is at least giving a more logical problem
So created_at should be filled in by reform right?
So in theory it should never be able to be null?
Alexey Palazhchenko
@AlekSi
In most situations, you want to use pointers for NULLable columns, and non-pointer fields for NOT NULL columns
Maran
@maran
Well created_at should never be null
Especially not if reform fills that in magically for me
Alexey Palazhchenko
@AlekSi
For that you could use BeforeInsert()
A somewhat related issue: go-reform/reform#146
Maran
@maran
Ok then I was confused; I expected this to work like in Rails (where all updated/created at fields always get inserted automagically)
Maran
@maran
Awesome, thanks for the pointers!
Maran
@maran
Sadly that does not have seem to solve it. I have proper timestamp in my database now but I still get errors when I try to retrieve the record.
Will go over the other stuff you linked
Alexey Palazhchenko
@AlekSi
Can you try without reform, with normal database/sql?
Maran
@maran
Let's try :)
Maran
@maran
Same error: sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *time.Time
I'm guessing this means it's not a reform issue
Alexey Palazhchenko
@AlekSi
Yeah
What you can do is to have two fields and AfterFind() hook:
//reform:artists
type Artist struct {
    ID   int32  `reform:"id,pk"`
    Name string `reform:"nameĀ»`
    CreatedAt *int32  `reform:"created_at"`
    UpdatedAt *int32 `reform:"updated_atĀ»`
    CreatedAtTime *time.Time
    UpdatedAtTime *time.Time
}

func (a *Artist) AfterFind() error {
  // set CreatedAtTime from CreatedAt
}
Alexey Palazhchenko
@AlekSi
@maran ^
Simon Kamenetskiy
@skamenetskiy
Hi
Alexey Palazhchenko
@AlekSi
Hey!
Simon Kamenetskiy
@skamenetskiy
Do you have any examples for pagination using reform?
Alexey Palazhchenko
@AlekSi
Reform does not provide any specific helper for that, sorry. You can get all rows and slice them (if it is effective enough for you), or you can use SQL tail with LIMIT/OFFSET and use generated code to scan results
Simon Kamenetskiy
@skamenetskiy
Yeah, I did that already, counting totals is what interests atm. Didn't find any shortcuts for that either.
Came up with this for now:


// Count is a handy method for counting number of rows for a query
func (q *Querier) Count(table Table, tail string, args ...interface{}) (int, error) {
    query := q.startQuery("SELECT COUNT(*) AS `cnt` FROM %s %s")
    rows, err := q.Query(fmt.Sprintf(query, q.QualifiedView(table), tail), args...)
    if err != nil {
        return 0, err
    }
    if rows.Next() {
        cnt := 0
        if err := rows.Scan(&cnt); err != nil {
            return 0, err
        }
        return cnt, nil
    }
    return 0, rows.Err()
}
Alexey Palazhchenko
@AlekSi
Well, you can simplify that code with QueryRow
Simon Kamenetskiy
@skamenetskiy
Something like this:
// Count is a handy method for counting number of rows for a query
func (q *Querier) Count(table Table, tail string, args ...interface{}) (int, error) {
    query := q.startQuery("SELECT COUNT(*) AS `cnt` FROM %s %s")
    cnt := 0
    if err := q.QueryRow(fmt.Sprintf(query, q.QualifiedView(table), tail), args...).Scan(&cnt); err != nil {
        return 0, err
    }
    return cnt, nil
}
Alexey Palazhchenko
@AlekSi
Yes
That sounds useful. Can you send a PR?
Simon Kamenetskiy
@skamenetskiy
Sure, let me add a unit test first
Alexey Palazhchenko
@AlekSi
Just create it on top of v1-4 branch
Simon Kamenetskiy
@skamenetskiy
Any tips on how to run tests locally? How do I work around the import path "gopkg.in..."?
Alexey Palazhchenko
@AlekSi
Install code with go get -u gopkg.in/reform.v1, change directory to $GOPATH/src/gopkg.in/reform.v1
Add git remote with your fork
Simon Kamenetskiy
@skamenetskiy
Thanks