Live data from Hacker News

Python: Just Write SQL

joaodlf.com

261–270 of 288 posts

Re: Python: Just Write SQL

#261

Earlier quoted context omitted.

Perhaps it depends what you're doing? IMO: .where('column_a', '=', 'value1') .and(q => q.isNull('column_b').orWhere('column_b', '=', 'value2'))) is a lot less readable than: WHERE column_a = 'value1' AND (column_b IS NULL OR column_b = 'value2')

One of these my IDE can typecheck and apply code hightlighting, the other is just a blob of text.

On my case that would be SQL, as I use nice SQL aware IDEs for Oracle and SQL Server.

Re: Python: Just Write SQL

#262
post #52
post #7

If you're going to end up querying all the fields and putting them into a model like this dataclass anyways... Django can do that for you. If you're going to later pick and choose what fields you query on the first load, and defer other data til later.... Django can do that for you. If you're going to have some foreign relations you want to easily query.... Django can do that for you. If you're doing a bunch of joins…

> I am curious as to what a larger codebase with "just SQL queries all over" ends up looking like. I have to imagine they all end up with some (granted, specialized) query builder pattern. But I think my bias is influenced by always working on software where there are just so many columns per table that it would be way too much busywork to not use something. You end up writing a query per usecase, rather than writing…

That is what stored procedures are for.

Re: Python: Just Write SQL

#263
post #7

If you're going to end up querying all the fields and putting them into a model like this dataclass anyways... Django can do that for you. If you're going to later pick and choose what fields you query on the first load, and defer other data til later.... Django can do that for you. If you're going to have some foreign relations you want to easily query.... Django can do that for you. If you're doing a bunch of joins…

Add refactoring, migrations and testing to all the reasons you mentioned, and it quickly becomes an adapted case of Greenspun's Tenth Rule for ORMs.

Migration tooling and SQL testing predate ORMs, it is a matter of actually caring about their existence.

Re: Python: Just Write SQL

#264
post #82

This is just reimplementing Djangos ORM, but badly. ORM queries compose . That's why [Python] programmers prefers them. You can create a QuerySet in Django, and then later add a filter, and then later another filter, and then later take a slice (pagination). This is hugely important for maintainable and composable code. Another thing that's great about Djangos ORM is that it's THIN. Very thin in fact. The entire impl…

Just like stored procedures compose, like in any procedural programming language.

Re: Python: Just Write SQL

#265
post #67

Seems like there's 3 groups of opinions on ORMs: Firstly (1); "I want to use the ORM for everything (table definitions, indexes, and queries)" Then second (2), on the other extreme: "I don't want an ORM, I want to do everything myself, all the SQL and reading the data into objects". Then thirdly (3) the middle ground: "I want the ORM to do the boring reading/writing data between the database and the code's objects".…

I'm in a 4th camp: we should be writing our applications against a relational data model and _not_ marshaling query results into and out of Objects at all. Elaborations on this approach: - https://news.ycombinator.com/item?id=34948816 - https://github.com/papers-we-love/papers-we-love/blob/main/d... - https://riffle.systems/essays/prelude/

Still on the 4th camp?

Using stored procedures and triggers as much as possible.

Re: Python: Just Write SQL

#266
post #91

I'd take it a step further and move all SQL into stored procedures and call those using a functional interface. That's because of PostgreSQL's excellent stored procedure support, it might be harder with, e.g. MySQL. One major benefit of stored procedures, in addition to separation of concerns, is that you can declare them SECURITY DEFINER and give them access to tables the Python process doesn't (in a way reminiscent…

That is my favourite approach, when I am allowed to have the last work how the DB layer should be like.

Re: Python: Just Write SQL

#267
post #93
post #91

I'd take it a step further and move all SQL into stored procedures and call those using a functional interface. That's because of PostgreSQL's excellent stored procedure support, it might be harder with, e.g. MySQL. One major benefit of stored procedures, in addition to separation of concerns, is that you can declare them SECURITY DEFINER and give them access to tables the Python process doesn't (in a way reminiscent…

Last project we've explicitly decided to not have any stored procedures ever since you basically can't test nor deploy them in any sane way. I'm all ears how you make it work.

From the point of view from Oracle and SQL Server, it looks like a consequence of not being aware of proper tooling.

Re: Python: Just Write SQL

#268

Earlier quoted context omitted.

If you don't want to maintain several queries, you could write something like SELECT * FROM shoes WHERE (CASE WHEN :brand_id IS NOT NULL THEN brand_id = :brand_id ELSE TRUE END) AND (CASE WHEN :size IS NOT NULL THEN size = :size ELSE TRUE END) AND (CASE WHEN :style IS NOT NULL THEN style = :style ELSE TRUE END)

That is pretty good. Much more readable than gluing a bunch of strings together.

And if you want to make it even more readable, those inner expressions can be wrapped into functions.

Re: Python: Just Write SQL

#269
post #189

Earlier quoted context omitted.

That makes no sense. What are you interpolating? Some variable. And you now have to audit that THAT VARIABLE is safe.

> What are you interpolating? Some variable. Nope, I'm generally interpolating an inline expression consisting entirely of string literals.

> generally

Does not inspire confidence.

Re: Python: Just Write SQL

#270
post #82

This is just reimplementing Djangos ORM, but badly. ORM queries compose . That's why [Python] programmers prefers them. You can create a QuerySet in Django, and then later add a filter, and then later another filter, and then later take a slice (pagination). This is hugely important for maintainable and composable code. Another thing that's great about Djangos ORM is that it's THIN. Very thin in fact. The entire impl…

Can you explain a bit more about the Django ORM being very thin and easy to read? It does seem like the Django ORM is thin (from an architecture perspective), but it doesn't seem to be small, it seems to be pretty big. Maybe I'm not understanding it though, so here's what I see: The "ORM" part of Django seems to be everything in `django.db.models.Model`, which seems to require you to declare your Models as subclasses…

It supports several backends and lots of stuff including migrations. It's thin for what it does.

> For comparison, all of Flask

That's... not a reasonable comparison. Flask does basically nothing. Of course it's small. And it does something totally different so why compare?

Post reply on HN