News for hackers. Stuff that matters.
Re:Back to basics: Writing an application using Go and Postg... by armchairhacker on Monday November 22, 2021 @09:17PM (#29313916)
> I’ve had a bad experience with ORM in the recent past, and I generally recommend against introducing this sort of abstraction to code.

Yeah I don’t get ORMs.

Most ORMs advertise “type safety” and not having to learn SQL. But actually you’re just writing SQL code in a non-SQL language. Usually the type-safety is weak, and a good IDE will provide type-checking and even data-source checking to raw SQL anyways.

Converting complex object operations into relational operations is non-trivial. ORM objects auto-query and auto-update from the database, but these queries/updates are often unoptimal or happen at invalid times. It’s much easier to write and execute the SQL queries manually, and then auto-convert the results to/from regular objects.

Re:Back to basics: Writing an application using Go and Postg... by orangepanda on Tuesday November 23, 2021 @01:07AM (#29315023)
> write SQL queries manually, and then auto-convert the results to/from regular objects.

In any meaningul sized codebase, converting to/from objects will be used all over the place. Then someone adds an abstraction on top, and congratulations - there is now an inhouse orm.

Re:Back to basics: Writing an application using Go and Postg... by systemvoltage on Tuesday November 23, 2021 @01:16AM (#29315055)
That's not the same thing as ORM. ORM constructs queries based on arbitrarily complex request. Abstraction layers that we add on top of existing 'handcrafted' SQL queries is often expected and necessary. With base foundation rock solid, explicit and unchanging – you can build abstractions safely and with peace.
Re:Back to basics: Writing an application using Go and Postg... by theshrike79 on Tuesday November 23, 2021 @01:02AM (#29315005)
Every single project I've seen that has used an ORM has had to spend days debugging "why is this simple query pulling half the database" -bugs.
Re:Back to basics: Writing an application using Go and Postg... by henvic on Tuesday November 23, 2021 @05:38AM (#29316345)
2. This is why I'm pushing so hard to keep things with plain SQL at work. So far, another team has experimental with squirrel (a SQL query builder) and it looks sane, but I myself wouldn't introduce not even this.

In my previous company, I was forced to use an outdated forked version of gorm. I'd just do my best to do anything else other than leading with databases there. Running the test suite took minutes, and to run way fewer test cases. It was a nightmare.

Re:Back to basics: Writing an application using Go and Postg... by davidw on Monday November 22, 2021 @09:23PM (#29313956)
> Most ORMs advertise “type safety” and not having to learn SQL. But actually you’re just writing SQL code in a non-SQL language

I think you're doing it wrong if you end up doing really complex queries in your ORM. It's kind of a judgement call, but at a certain point, you need to switch over to SQL. Good ORM's let you do that without too much hassle.

Re:Back to basics: Writing an application using Go and Postg... by piyh on Monday November 22, 2021 @10:10PM (#29314202)
I wrote a django app using postgres full text search. I ended up going with direct sql calls for anything more than a single record get because of all the specialized syntax. It's nice to keep stuff like new record creation clean, but once you get more than one join deep it's time to break out the best domain specific language ever created.
Re:Back to basics: Writing an application using Go and Postg... by dboreham on Monday November 22, 2021 @10:45PM (#29314369)
ORM selling point: you can bypass it.
Re:Back to basics: Writing an application using Go and Postg... by davidw on Tuesday November 23, 2021 @11:31AM (#29319785)
Well, sure. It makes it easy to do simple stuff and gets out of the way when you want to bypass it rather than being from the "sorry Dave, I can't let you do that" school of interface design.
Re:Back to basics: Writing an application using Go and Postg... by fy20 on Tuesday November 23, 2021 @02:27AM (#29315424)
The only good ORM I've used is Rail's ActiveRecord. Sure there are gotchas you need to know about, maybe it's just experience, but I find these to be a non-issue.

Every other ORM in other languages I've tried seems to have WTF moments. It could be that Ruby is a dynamic enough language that it allows you to work around these things easily, that stricter languages do not.

Re:Back to basics: Writing an application using Go and Postg... by captainmuon on Tuesday November 23, 2021 @03:15AM (#29315693)
One benefit of using an ORM is that you don't write SQL strings, you write statements and method calls. So you get better IDE support, and detect syntax errors before they hit the database. If you are just writing a CRUD application, maybe with one or two `joins` or `order by`, then an ORM is perfect. If you are doing anything more complex, then by all means drop to plain SQL.

The real question to me is, why can't we drop the relational database? In many small projects (where you would use an ORM) it is overkill. Why can't I create a list of plain-old-data objects in my language, then add indicies on that, and do fast queries directly in the language? Most languages already have a data structure with an index on one key (a dict, hashmap or similar). With C#'s LINQ or Pythons list comprehensions we already have half of the solution.

Re:Back to basics: Writing an application using Go and Postg... by Zababa on Tuesday November 23, 2021 @03:57AM (#29315903)
From what I understand, data duplication can lead to consistency errors. And if you want to avoid data duplication, you're going to rewrite a relational database yourself, since you would want to put your data into normal forms to avoid duplication. I think I've read a few times on Hacker News that we already went through the alternatives to the relational database around the time of its birth, and that a few papers were published at the time on why this was a better solution than others.
Re:Back to basics: Writing an application using Go and Postg... by twoquestions on Tuesday November 23, 2021 @09:21AM (#29318103)
> a good IDE will provide type-checking and even data-source checking to raw SQL anyways

I've never in my life heard of this, which IDE are you talking about? I'd love to use something like that! I'm in C# land where I can use Management Studio to write queries which works well enough, but going between that and Visual Studio does have some friction.

Even though we use Entity Framework here, I mostly use it to manage migrations than to work with data. Most of the time unless a query is really trivial, I'll just write some SQL and use Dapper for object mapping, I find it a lot easier once the query gets to be over three or four lines or so.

Re:Back to basics: Writing an application using Go and Postg... by armchairhacker on Tuesday November 23, 2021 @09:43AM (#29318340)
IntelliJ IDEA. For example in Java if you put // language=SQL before a string literal or @Language(“SQL”) before a function parameter at declaration and call with the string literal, the literal text will be highlighted. You “configure SQL dialect” and “configure data source” to get it to point to your database
Re:Back to basics: Writing an application using Go and Postg... by chakkepolja on Tuesday November 23, 2021 @10:11AM (#29318689)
> auto-convert the results to/from regular objects.

Sadly this functionality, which I think is not much more complex than a JSON parser, is absent from many toolkits / libraries, and an immediate option people see is an ORM. Libraries that just do serialization / deserialization from SQL are much less popular than something like Hibernate, which could be a reason.

Re:Back to basics: Writing an application using Go and Postg... by dragonwriter on Monday November 22, 2021 @09:34PM (#29314007)
> Most ORMs advertise “type safety” and not having to learn SQL. But actually you’re just writing SQL code in a non-SQL language.

That's...not writing SQL code, just as advertised. In the same way that writing code in a higher-level language isn't writing native machine code.

Re:Back to basics: Writing an application using Go and Postg... by armchairhacker on Monday November 22, 2021 @09:56PM (#29314127)
The difference is you’re still writing SQL keywords, just as functions

e.g. userPosts = Posts.InnerJoin(Users).SelectAll().Where(user => user.id == userID)

It’s not always exactly the same, but the point remains that you have to understand SQL anyways and the code usually isn’t any clearer.

Maybe i’m confusing this with query builders. ORMs also let you form queries by interacting with objects directly, e.g. ‘user = Users.Find(userID); userPosts = user.Posts’. But that leads to the other issue with inefficient and unpredictable queries.

Re:Back to basics: Writing an application using Go and Postg... by mdavis6890 on Monday November 22, 2021 @11:54PM (#29314744)
As a person that writes my own SQL and hasn't had much/any use for ORMs in the past - I have the imagination that if you were using an ORM you could switch the underlying database without a lot of trouble.

Is there any truth to this?

Re:Back to basics: Writing an application using Go and Postg... by thraxil on Tuesday November 23, 2021 @05:47AM (#29316375)
Yes and no. I've used Django and Django's ORM for many years (along with others, but Django is probably the easiest for me to discuss).

Django's ORM doesn't stop you from using database-specific features (JSON fields, full text search, PostgreSQL's geo extensions, etc.) It also makes it very straightforward to bypass parts of the ORM and write your own SQL either just as the WHERE part of a query or as the whole SQL statement. If you build a large enough app that deals with enough data, you'll eventually find some hot spots where you need to do that for performance reasons. When you do that, you obviously can lose portability.

If you avoid those things though, you pretty much can switch the database. In practice, I've never really found a need to do that for a production system, but it still has some major advantages. First, I regularly run PostgreSQL in production, but I can use SQLite as an in-memory database for unit tests, which makes those much faster (and simpler to run unit tests without having to also spin up a full DBMS).

The other huge advantage is that it enables Django's whole ecosystem of reusable applications. Eg, the built-in admin interface, popular third party applications for authentication, tagging, CMS-type stuff, etc. are generally written using the ORM and as a result can be added to any Django project with a couple lines of config and will work no matter what database you use. That large ecosystem of fairly polished components has been a major reason that Django has remained so popular for such a long time. If it had to be split up by supported database ("MySQL admin" vs "PostgreSQL admin", etc), I don't think it would've been as successful.

Re:Back to basics: Writing an application using Go and Postg... by jmnicolas on Tuesday November 23, 2021 @03:03AM (#29315621)
I think the database change thing is way overrated. I'm 12 years in the same company, I juggle with Postgres, MySQL, SQL Server (and Oracle previously).

Most of the databases I access are from external providers. We changed our main provider 2 years ago, so their DB went from Oracle to SQL Server.

So in 12 years that would have been one database change. Except all the schema has changed and they don't allow SQL queries on it but provide good old SOAP web services...

So even if I had used a full ORM (I use Dapper, you write the SQL, it "just" converts the result of the query to objects) I still would have to change my code to adapt to the new provider.

However going from SQL to webservices really convinced me SQL is a superpower. With it you get exactly the data you need, no need to re-process it in code.

Before this change I never had to really look at my apps performances, it was good enough. But since this provider webservices are so inefficient (for example I can't "query" a single customer, I have to get all of them) I had to optimize my app in every possible way to mitigate these inefficiencies.

Re:Back to basics: Writing an application using Go and Postg... by armchairhacker on Tuesday November 23, 2021 @12:24AM (#29314866)
It depends on how big the switch is.

I assume it would be a bit easier because the query and ORM syntax is uniform across databases. And it also probably wont support many database-specific operations.

But it would still be work. Some databases don’t support basic operations (e.g. UPDATE with LIMIT in postgres) which you can still do with the query syntax. And different databases usually have different preferred (usually faster) ways of doing indexes, joins etc. Plus you still have to migrate your data.

ORMs are ideally supposed to be a complete abstraction where you can pretend the database doesn’t exist, so if you switch it shouldn’t matter. The problem in, in practice you can’t pretend the database doesn’t exist. So you either end up writing code for your specific dialect anyways, or you write inefficient code.

It’s also like, you can just write plain SQL and ignore your dialect’s extra features. And that would make migration much easier, but it would also be less efficient and much harder to write.

Re:Back to basics: Writing an application using Go and Postg... by pphysch on Tuesday November 23, 2021 @12:18AM (#29314829)
When has anyone ever a) had to switch from RDBMS to RDBMS in a production system and b) it not be a massive pain in the neck?
Re:Back to basics: Writing an application using Go and Postg... by dboreham on Monday November 22, 2021 @10:44PM (#29314364)
If you think an ORM is dumb, you haven't tried an FRM.
Re:Back to basics: Writing an application using Go and Postg... by jmnicolas on Tuesday November 23, 2021 @03:05AM (#29315636)
What is a FRM? A quick search didn't get me anything relevant to databases.