There are many good ORMs for Go. One good list can be found 
at https://awesome-go.com/#orm

I think ORMs definitely have their place, and I have made good use of 
NHibernate in the .NET environment and Hibernate in Java. Having said that, 
I have found that for almost all of the work I have done in SQL with Go I 
prefer to avoid the additional layer of an ORM if I can. In doing this, 
though, I have run into exactly the same problem as you describe. The 
`sqlx` package does a creditable job of handling SELECT statements, but is 
not as much help when preparing INSERT and UPDATE statements for tables 
with a large number of columns.

I have tried a number of different approaches, but to date I have had most 
success with the `sqlrow` package at https://github.com/jjeffery/sqlrow 
(shameless plug, sorry). This package works directly with the database/sql 
package -- there is no additional layer. This allows one to mix and match: 
for example use package sqlx where it is most useful, and use package 
sqlrow in other places if it is of help.

In your example, you have a structure that looks something like:

type Bla struct {
    Bla int
    Bla1 string
    Bla2 string
    // ... and so on ...
    BlaN int
}

Using the sqlrow package you would insert a row by calling

    var bla := &Bla{
        // ... initialize members here ...
    }

    err := sqlrow.Insert(db, bla, "blas")

If you later add another column to the "blas" table, you add another field 
to the "Bla" type and then the sqlrow package will update the SQL 
accordingly.

When selecting rows you can use '{}' instead of your column list, and the 
sqlrow package will insert the columns for you.

    var blas []*Bla

    n, err := sqlrow.Select(db, &blas, "select {} from blas where bla2 = ? 
and bla3 = ? order by {}", bla2, bla3) 

Note that most ORMs will do this and more. Others readers may be able to 
share their experiences and recommendation for using specific ORMs. It is 
just a personal preference, but I just like being able to keep close to the 
database/sql package without the need for any additional layers.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to