News for hackers. Stuff that matters.
Re:Seven years of Go by tptacek on Thursday November 10, 2016 @10:40AM (#12921286)
Strong, strong disagree on the idea that ORMs aren't necessary because of some property of Golang itself. Lack of decent ORMs is probably my biggest complaint about Go, and I think if you look at a lot of large web app packages you'll find people are all writing their own 40-60% of an ORM.
Re:Seven years of Go by oelmekki on Thursday November 10, 2016 @11:04AM (#12921537)
When I started using go (coming from rails), ORM was really the thing I missed the most. And this made me realize I actually didn't know much about postgres.

I started using pg functions, views, triggers, etc and found it extremely convenient. Now, my sql strings in go are mostly `SELECT * FROM func_name(<$params>)` and I'm happy with that.

I'm not saying ORMs are useless (active_record is an incredibly cool tool), but it's been surprisingly way easier to live without them that I'd thought.

Re:Seven years of Go by tptacek on Thursday November 10, 2016 @11:43AM (#12921926)
Even writing Postgres functions, you're still writing the same basic SQL statements (for the 80% of your database interactions that are essentially CRUD) over and over again.

People used to defend writing things in assembly language for similar reasons, as a way of getting more machine sympathy. I'm not buying it.

Re:Seven years of Go by tedunangst on Thursday November 10, 2016 @12:20PM (#12922238)
I'll argue this point. It takes me 30 seconds to write a SQL query. It's not hard. On the flip side, I don't end up running queries that haul megabytes of data in just to peek at one column.

SQL is already much higher level than assembly. Piling on more abstraction just increases the likelihood that the code is doing extraneous work.

Re:Seven years of Go by dasil003 on Thursday November 10, 2016 @12:41PM (#12922472)
I agree SQL is not hard, but getting the data into objects is definitely repetitive work. ORMs are a double-edged sword where people who don't know SQL write bad ORM code because they can, but a good ORM gives various ORM level that allow one to do the right thing and still save a lot of repetitive boilerplate.

If you avoid an ORM, the typical medium sized CRUD app usually ends up with some poorly conceived bespoke abstractions that would be better used by some flavor of ORM. I'm with you that ORMs make it way too easy for junior devs to blow their leg off, but I still think they provide some very solid abstractions when used correctly.

Re:Seven years of Go by ansible on Thursday November 10, 2016 @04:42PM (#12924679)
If you are doing plain SQL in your golang app, I highly encourage you to check out sqlx:

https://github.com/jmoiron/sqlx

It has features like directly unpacking the results of a query into a structure. Very handy.

Re:Seven years of Go by _ph_ on Thursday November 10, 2016 @03:17PM (#12923892)
Fully agree. ORMs save you typing if you just need to wrap a table, but for complex multi-table queries I have seen really bad performance as a result. I rather prefer to capture my queries in a view, and then wrapping that is so trivial, that I don't need an ORM.
Re:Seven years of Go by oelmekki on Thursday November 10, 2016 @11:53AM (#12921998)
> People used to defend writing things in assembly language for similar reasons, as a way of getting more machine sympathy. I'm not buying it.

I have to assume those people did not first learn and use C, then went to assembly, but rather the opposite ;) So, the comparaison does not apply, here.

> you're still writing the same basic SQL statements (for the 80% of your database interactions that are essentially CRUD)

It's not the case, actually. My functions are way more expressive that the crud I did with orms. With PL/pgSQL, I group several queries in a same pg function, I always do more that a simple insert or update. I also fine tune my returned fields - which most of the time are composite from several tables / computations.

This is really outsourcing all data manipulation to the database, the comparison that applies is more with a presenter/serializer than with an orm model.

Re:Seven years of Go by neospice on Thursday November 10, 2016 @12:06PM (#12922108)
What are the typical trade-offs of outsourcing all the data manipulation & lookups to the DB. Are there cases when this is absolutely what you don't want to do?

I realize this is a very general and vague question but it's something I've been pondering about.

Re:Seven years of Go by oelmekki on Thursday November 10, 2016 @12:21PM (#12922257)
By far, it is dealing with migrations.

Initially, I would use `\ef <func_name>` in psql and just write and try my function, but this turned out to be not as pleasant as writing code in an editor in a dedicated file. The annoying thing with pg functions is that, if you want to replace them, you have to provide exactly the same in and out parameters. This means that if you want to change the parameters, you have to drop the function and create a new one.

This has been annoying at first, but now I'm more comfortable with it. I write my functions as migration files [1] and just migrate/rollback to do my tests. I guess I started to give more attention to the input and output parameters from the start in the mean time.

The cases where I won't use functions is when I want to do something extremely simple, like `SELECT email FROM users WHERE id = 1`. It's just not worth writing a function for that. It doesn't happen often that this is all I need to process my request either.

[1] I use https://github.com/mattes/migrate

Re:Seven years of Go by jryhurst on Thursday November 10, 2016 @12:34PM (#12922406)
We prevented the grossness of writing things over and over again by just using Golang's script generation and some other stuff. http://github.com/spacemonkeygo/dbx.
Re:Seven years of Go by tptacek on Thursday November 10, 2016 @12:58PM (#12922614)
I wrote something very similar, but this is exactly what I mean by writing 40% of an ORM.
Re:Seven years of Go by stcredzero on Thursday November 10, 2016 @02:35PM (#12923476)
I find this interesting. Are there examples around, or a blog post discussing this?
Re:Seven years of Go by aleksi on Thursday November 10, 2016 @04:20PM (#12924480)
Also see https://github.com/go-reform/reform for a stable implementation of a similar idea.
Re:Seven years of Go by aleksi on Thursday November 10, 2016 @04:08PM (#12924356)
That's why we created https://github.com/go-reform/reform. It gives you type-safe (without interface{}) helpers for both common cases and reading custom query results.
Re:Seven years of Go by sethammons on Thursday November 10, 2016 @11:05AM (#12921551)
One of my biggest complaints about ORMs is that I often know the exact query I need to write, and it is a pain to compose it with the ORM. Simple CRUD, sure, maybe. Beyond that though... Arguably one of the biggest benefits of an ORM is sanitization. You get that for free with Go. I've done quite a bit of work with DBs and Go, and I've not seen the need for an ORM, just some library helpers.
Re:Seven years of Go by aikah on Thursday November 10, 2016 @12:01PM (#12922072)
> You get that for free with Go

You get that for free in any language that allows you to pass parameters to a query. There is nothing Go specific.

> One of my biggest complaints about ORMs is that I often know the exact query I need to write, and it is a pain to compose it with the ORM.

I agree that ORMs are often their own DSL one has to learn, when SQL is already there. In that sense they are opaque. Yet ORM still deal with relationships that have to be manually wired with queries, since AFAIK SQL query are still 2 dimensional. In my opinion query builders are often a nice trade-off between a fully featured ORM and raw SQL : you get SQL abstraction thus query reuse at minimal cost without the complexity.

An other alternative is Jooq style code generation/scaffolding but each time a tool like that is added to a compilation pipeline it is yet another dependency to manage. Code generation is opaque in its own ways.

Finally there is the nuclear option in shape of HQL but it sounds like a half baked farce to go back to "SQL" to avoid SQL.

I myself chose to go with query builders, in fact i'm porting Doctrine DBAL to Go as we speak.

Re:Seven years of Go by thewhitetulip on Thursday November 10, 2016 @01:01PM (#12922635)
I have written a small webapp in Go while learning it and I didn't use an ORM, but the feedback I got from reddit is that at some point in time I'd need to use an ORM. What are your thoughts on the usage of ORM? +ve points vs -ve points?
Re:Seven years of Go by didip on Thursday November 10, 2016 @11:56AM (#12922020)
Wrapper functions are not the same as full blown ORM.

For every heavily used ORM features, there's an opportunity to fall into a trap, e.g N+1, because the surface area of an ORM is wide.

In contrast, wrapper functions are low cost, easy to write, and recognizable because you write them yourself.

Re:Seven years of Go by pcwalton on Thursday November 10, 2016 @01:16PM (#12922746)
> In contrast, wrapper functions are low cost, easy to write, and recognizable because you write them yourself.

Isn't saying that you should use functions that are "recognizable because you write them yourself" basically saying "you shouldn't use libraries?" This seems like the obvious NIH fallacy.

Re:Seven years of Go by fauigerzigerk on Thursday November 10, 2016 @04:26PM (#12924545)
Not necessarily. It could also mean that these functions are more specific, have names that make sense in the concrete context and don't have any unnecessary parameters you need to think about. When it comes to ORMs it's even worse. They are often not libraries but frameworks with tons of non-local settings and state you need to know about (or else...).

Not invented here also means not invented for the specific task at hand but for a much more general set of use cases. In my view it's a trade-off, not a fallacy.

Re:Seven years of Go by randomdata on Thursday November 10, 2016 @11:31AM (#12921828)
In my experience there is one property of Go that minimizes the need for a good ORM, and that is that the problem domain Go excels in does not have great overlap with the problem domain that ORMs excel in. What interfacing with a relational database does come up is of the nature that does not fit well into the ORM model anyway.

Of course, you can write software in the same domain where ORMs excel in Go, but I am not sure it is the best tool for that job. A good ORM would go a long way to making that less true, but I'm not sure even that would put Go at the same level as tools that are geared towards that type of problem. Go, like all tools, is still targeted at a certain class of problems.

Re:Seven years of Go by weberc2 on Thursday November 10, 2016 @12:39PM (#12922449)
What domain are you referring to?
Re:Seven years of Go by ben_jones on Thursday November 10, 2016 @03:53PM (#12924241)
Not GP but my view is that Go excels in "split-backends" where you kind of have a front-end server and a back-end server where the front-end is taking in requests and passing them to a queue or interacting with services representing the "backend's backend". In this regard you're usually building for scale and high optimizing your code to be incredibly concise and to the point. An ORM doesn't belong here because you've already decided software quality > developer productivity within such an important capacity-driven system.
Re:Seven years of Go by sethammons on Thursday November 10, 2016 @05:26PM (#12925100)
I find the majority of what you contribute to HN very insightful; I appreciate your activity here! I've yet to use an ORM that I've found added much or any value, and usually I have to fight it. I find "select id from user where email='[email protected]'" much easier to work with than some odd query build that requires some DSL that made sense to some developer for their particular use case, like user.Get(id).Where().Email("[email protected]). My most recent case of growing disdain for ORMs being SQLAlchemy. What ORMs do you find (in any language) that you like and what is it about them that helps you?
Re:Seven years of Go by vram22 on Thursday November 10, 2016 @11:29AM (#12921799)
>Strong, strong disagree on the idea that ORMs aren't necessary because of some property of Golang itself.

Might be interesting to hear reasons - by both you and the parent commenter.