TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python: Just Write SQL

223 pointsby joaodlfalmost 2 years ago

58 comments

zzzeekalmost 2 years ago
ORMs do much more than &quot;write SQL&quot;. This is about 40% of the value they add.<p>As this argument comes up over, and over, and over, and over again, writers of the &quot;bah ORM&quot; club continuously thinking, well I&#x27;m not sure, that ORMs are just going to go &quot;poof&quot; one day? I wrote some years back the &quot;SQL is Just As Easy as an ORM Challenge&quot; which demonstrates maybe a few little things that ORMs do for you besides &quot;write SQL&quot;, like persisting and loading data between classes and tables that are joined in various very common ways to represent associations between classes:<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;zzzeek&#x2F;5f58d007698c4a0c372edd95ab8e0267" rel="nofollow noreferrer">https:&#x2F;&#x2F;gist.github.com&#x2F;zzzeek&#x2F;5f58d007698c4a0c372edd95ab8e0...</a><p>this is why whenever someone writes one of these &quot;just write SQL&quot; comments, or wow here a whole blog post! wow. I just shake my head. Because this is not at all what the ORM is really getting you. Plenty of ORMs let you write raw SQL or something very close to it. The SQL is not really the point. It&#x27;s about the rows and objects, moving the data from the objects to the INSERT statement, moving the data from the rows you SELECTed back to the objects. Not to mention abstraction over all the other messy things the database drivers do like dealing with datatypes and stuff like that.<p>It looks like in this blog post, they actually implemented their own nano-ORM that stores one row and queries one table. Well great, now scale that approach up and see how much fun it is to write the same boilerplate XYZRepository &#x2F; XYZPostgresqlRepository code with the same INSERT &#x2F; SELECT statement over, and over again. I&#x27;d sure want to automate all that tedium. I&#x27;d want a one-to-many collection too maybe.<p>You can use SQLAlchemy (which I wrote) and write all the SQL 100% yourself as literal strings, and still use the ORM, and still be using an enormous amount of automation to deal with the database drivers and moving data between your objects and rows. But why would anyone really want to, writing SQL for CRUD is really repetitive and tedious. Computer can do that for you.
评论 #37122544 未加载
评论 #37124480 未加载
评论 #37121656 未加载
评论 #37121868 未加载
评论 #37121766 未加载
评论 #37122997 未加载
评论 #37123248 未加载
评论 #37125418 未加载
评论 #37122830 未加载
评论 #37122329 未加载
评论 #37122155 未加载
评论 #37123449 未加载
评论 #37127899 未加载
评论 #37121615 未加载
评论 #37126599 未加载
评论 #37131664 未加载
评论 #37121874 未加载
评论 #37125793 未加载
评论 #37127371 未加载
评论 #37125122 未加载
评论 #37122243 未加载
rtpgalmost 2 years ago
If you&#x27;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&#x27;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&#x27;re going to have some foreign relations you want to easily query.... Django can do that for you. If you&#x27;re doing a bunch of joins and are using some custom postgres extensions for certain fields and filtering... Django can help you organize the integration code cleanly.<p>I totally understand people having issues with Django&#x27;s ORM due to the query laziness making performance tricky to reason about (since an expression might or might not trigger a query depending on the context). In some specialized cases there are some real performance hits from the model creation. But Django is very good at avoiding weird SQL issues, does a lot of things correctly the first time around, and also includes wonderful things like a migration layer.<p>You might have a database that is _really_ unamenable to something like an ORM (like if most of your tables don&#x27;t have ID rows), but I wonder how much of the wisdom around ORMs is due to people being burned by half-baked ORMs over the years.<p>I am curious as to what a larger codebase with &quot;just SQL queries all over&quot; 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.
评论 #37119336 未加载
评论 #37119703 未加载
评论 #37119598 未加载
评论 #37121421 未加载
评论 #37119893 未加载
评论 #37119507 未加载
评论 #37122711 未加载
评论 #37120168 未加载
评论 #37121126 未加载
boxedalmost 2 years ago
This is just reimplementing Djangos ORM, but badly.<p>ORM queries <i>compose</i>. That&#x27;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.<p>Another thing that&#x27;s great about Djangos ORM is that it&#x27;s THIN. Very thin in fact. The entire implementation is pretty tiny and super easy to read. You can just fork it by copying the entire thing into your DB if you want.
评论 #37122303 未加载
评论 #37121067 未加载
评论 #37125939 未加载
评论 #37124473 未加载
评论 #37131075 未加载
zknillalmost 2 years ago
Seems like there&#x27;s 3 groups of opinions on ORMs:<p>Firstly (1); &quot;I want to use the ORM for everything (table definitions, indexes, and queries)&quot;<p>Then second (2), on the other extreme: &quot;I don&#x27;t want an ORM, I want to do everything myself, all the SQL and reading the data into objects&quot;.<p>Then thirdly (3) the middle ground: &quot;I want the ORM to do the boring reading&#x2F;writing data between the database and the code&#x27;s objects&quot;.<p>The problem with ORMs is that they are often designed to do number 1, and are used to do number 3. This means there&#x27;s often &#x27;magic&#x27; in the ORM, when really all someone wanted to do was generate the code to read&#x2F;write data from the database. In my experience this pushes engineers to adopt number 2.<p>I&#x27;m a big fan of projects like sqlc[1] which will take SQL that you write, and generate the code for reading&#x2F;writing that data&#x2F;objects into and out of the database. It gives you number 3 without any of the magic from number 1.<p>[1] <a href="https:&#x2F;&#x2F;sqlc.dev&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;sqlc.dev&#x2F;</a>
评论 #37123638 未加载
评论 #37123452 未加载
评论 #37120997 未加载
评论 #37122648 未加载
评论 #37134522 未加载
评论 #37124486 未加载
NewEntryHNalmost 2 years ago
Any serious application beyond the example given in this article will include conditional SQL constructs which go beyond SQL query parameters and will therefore require string formatting to build the SQL.<p>Think a simple UI switch to sort some result either ascending or descending, which will require you format either an `ASC` or a `DESC` in your SQL string.<p>The moment you build SQL with string formatting is the moment you&#x27;re rewriting the SQL formatter from an ORM, meeting plenty of opportunities to shoot yourself in the foot.
评论 #37119607 未加载
评论 #37119386 未加载
评论 #37119444 未加载
评论 #37123499 未加载
评论 #37123650 未加载
Rudismalmost 2 years ago
The next logical step after writing the code given in the article is to abstract common boilerplate SQL into a library so you&#x27;re not spending 50% of your time writing and re-writing basic SQL insert, update, and select statements every time your models need to be updated. At which point all you&#x27;ve done is write your own ORM.<p>If you want to go full-blown SQL you can use something like PostGraphile, which allows you to create all of your business entities as tables or views and then write all your business logic as SQL stored procedures, which get automatically translated into a GraphQL API for your clients to consume, but once you move beyond basic CRUD operations and reporting it becomes incredibly difficult to work with since there aren&#x27;t really any good IDEs that help you manage and navigate huge pure-SQL code bases.<p>If you&#x27;re really dead set against using a powerful ORM, it&#x27;s probably still a good idea to find and use a lightweight one--something that handles the tedious CRUD operations between your tables and objects, but lets you break out and write your own raw queries when you need to do something more complex. I think there&#x27;s a sweet spot between writing every line of SQL your application executes and having an ORM take care of boilerplate for you that will probably be different in every case but will never be 100% at one end or the other.
评论 #37123355 未加载
bbojanalmost 2 years ago
The article is missing the code for creating the &quot;users&quot; database table. What about indexes? Migrations? Relations to other tables?<p>I mean you can just write SQL instead of using the ORM if your project consists of a single table with no indexes that will never change, sure.
评论 #37119383 未加载
评论 #37121276 未加载
评论 #37119401 未加载
评论 #37119378 未加载
promiseofbeansalmost 2 years ago
&gt; ... Python dot not have anything in the standard library that supports database interaction, this has always been a problem for the community to solve.<p>Python has built-in support for SQLite in the standard library: <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;sqlite3.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;sqlite3.html</a>
评论 #37119302 未加载
评论 #37119301 未加载
评论 #37131671 未加载
baqalmost 2 years ago
It&#x27;s fine advice... if you don&#x27;t ever need to build queries programmatically (you will, probably) and don&#x27;t care about type checks (you should, it&#x27;s 2023).<p>If you don&#x27;t know what you&#x27;re doing on the DB-app interface, you&#x27;re still better off with an ORM most of the time. If you don&#x27;t know if you know, you don&#x27;t know (especially if you <i>think</i> you know but details are fuzzy); please go read sqlalchemy docs, no, skimming doesn&#x27;t count.<p>If you know what you&#x27;re doing but are new to Python, use sqlalchemy.core.<p>PS. zzzeek is a low-key god-tier hacker.
评论 #37123159 未加载
Jackevansevoalmost 2 years ago
&gt; I have spent enough time in tech to see languages and frameworks fall out of grace, libraries and tools coming and going.<p>I feel like Django ORM and SQLAlchemy are the de-facto ORMs for Python and have been around for over a decade. If anything I&#x27;d recommend juniors to pick one of these over hand rolling their own solution because it&#x27;s so ubiquitous in the ecosystem.
ckdotalmost 2 years ago
Congrats, you just wrote your own ORM. Please mind that ORM doesn’t necessarily mean ActiveRecord, which could be considered an anti pattern.
评论 #37119220 未加载
评论 #37119170 未加载
dotdialmost 2 years ago
Yes, this!<p>I&#x27;ve been burned countless times by Hibernate (and consorts) and now I argue in favour of plain SQL wherever I can. I do not imply that Hibernate is in itself bad, I just have collected many years of observations about projects built upon it, and they all had similar problems regarding tech debt and difficult maintenance, and most of them sooner or later ran into situations where Hibernate had to be worked around in very ugly ways.<p>Yes, I can understand some of the arguments for ORMs, especially when you get a lot of functionality automagically à la Spring Boot repositories.<p>And since nowadays I have more influence, I do advocate for plain SQL or - the middle ground - projects like jOOQ, but without code generation, without magic, just for type safety. We&#x27;ve been quite happy with this approach for a very large rewrite that is now being used productively with success.
badcppdevalmost 2 years ago
If you are just going to &quot;Just Write SQL&quot; then I really don&#x27;t think you should be coding your own Object and Repository classes.<p>My vision of the &quot;Just Write SQL&quot; paradigm would be a &quot;class&quot; or equivalent that would take a SQL command and return the response from the server. Obviously the response has a few different forms but if you&#x27;re &quot;just writing SQL&quot; then those responses are database responses and not models or collections of models.<p>(For the record I think simple ORM type functionality is actually quite useful as your use case moves past the scale of small utility scripts.
fmajidalmost 2 years ago
I&#x27;d take it a step further and move all SQL into stored procedures and call those using a functional interface. That&#x27;s because of PostgreSQL&#x27;s excellent stored procedure support, it might be harder with, e.g. MySQL.<p>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&#x27;t (in a way reminiscent of setuid), thus improving the security posture dramatically.<p>One example: you could have a procedure authenticate(login, password) that has access to the table of users and (hashed) passwords, but if the Python app server is compromised it doesn&#x27;t have access to even the hashed passwords or even the ability to enumerate users of the system.
评论 #37120955 未加载
评论 #37131089 未加载
tantamanalmost 2 years ago
I was sad to find that the author proclaimed &quot;just write SQL&quot; then fell into the trap of modeling his data as objects.<p>If you&#x27;re going to model your data this way... you might as well use an ORM.<p>A better way is to just just write SQL (or datalog) and model our data, from DB all the way up to the application, as relations rather than objects.<p>Rather than re-hash, this idea has previously been discussed on HN here: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=34948816">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=34948816</a>
davidthewatsonalmost 2 years ago
I&#x27;m happy to see someone mention peewee, having used it for numerous startup prototypes since its inception.<p>Coleifer does not get enough credit IMHO:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;coleifer&#x2F;">https:&#x2F;&#x2F;github.com&#x2F;coleifer&#x2F;</a><p>Peewee has been solid since I began using it a decade ago. Coleifer&#x27;s stewardship is hard to see at once, but I&#x27;ve interacted with him numerous times back then and the software reflects the mindset of its creator.
评论 #37123542 未加载
评论 #37122550 未加载
hot_grilalmost 2 years ago
You don&#x27;t need an ORM, but this isn&#x27;t how you avoid one. If you&#x27;re thinking of your DB as a mere object store &#x2F; OOP connector like this article is, you&#x27;re better off with an ORM or NoSQL than this basically equivalent DIY solution. It&#x27;s best to instead learn how to use a relational DB like a relational DB, and the rest will follow.<p>Also, I&#x27;m not one of those people who dislike easy things (and will often whine about JS or Python existing). I&#x27;m all for ease and focusing on the business goals. It&#x27;s just that ORMs and bad schema design will make things harder.
risalmost 2 years ago
I&#x27;ve been both ways on this, and ultimately I come down heavily in the camp of using ORMs for as far as it makes sense. Why? Sure, the &quot;just use SQL because it&#x27;s so simple&quot; crowd use seductively simple examples, and indeed for very static use-cases it can be quite neat and simple.<p>But projects (almost always) grow, and once you need to start conditionally adding filter clauses, conditionally adding joins, things start to get <i>very</i> weird <i>very</i> fast. And no, letting the database connector library do the quoting for you <i>won&#x27;t</i> save you from SQL injection attacks unless you&#x27;re just using it to substitute primitive values.<p>And once all your logic is having to spend more space dealing with conditional string formatting, the clarity of what the query is actually trying to do is long gone.<p>I&#x27;ll refrain from digging up the piece of code where I was having to get the escaping correct for a field that was embedded SQL-in-SQL-in-SQL-in-go. And I could hear the echos of the original author&#x27;s &quot;YAGNI&quot;s haunting me.
robertlagrantalmost 2 years ago
With SQLAlchemy, I come for the type checking. I stay for the Alembic migrations.
评论 #37119493 未加载
评论 #37119225 未加载
WesolyKubeczekalmost 2 years ago
My god, object-relational impedance mismatch seems to be more polarizing than US politics. I&#x27;ve been observing this field for more than 15 years, and it&#x27;s always &quot;JUST WRITE SQL!!!!!&quot; versus &quot;DRY!!! DRY!!! USE ORMs SO YOU DON&#x27;T HAVE TO WRITE SQL!!!! BUSINESS LOGIC!!!&quot; shouting matches. It&#x27;s like this topic itself takes 30 IQ points away from each participant and the conversation then devolves into complete chaos.<p>Maybe there&#x27;s some professional trauma at work, as many of us have been traumatized by shitty databases and shitty code working with them alike, ORM or not. But ORMs do come and go, promise bliss, deliver diddly, and I&#x27;m reading the same stuff I&#x27;ve been reading in 2008, as if nothing ever changed since.
sergioisidoroalmost 2 years ago
I&#x27;ve used Rails AR and Django&#x2F;SQL Alchemy orms, and the more I use it, the more I wish for a fusion of both.<p>Django ORM is amazing for Schema management and migrations, but I dislike their query interfaces (using &quot;filter&quot; instead of &quot;where&quot;). I really like Rails AR way of lightly wrapping SQL, with a almost 1-1, and similar names, but does not have a migration manager - and there is always the chance that your schema and your code will diverge.<p>If I would get a Schema &#x2F; migration manager, that would allow to do type checks and that would work well with a language server for autocompletes, but use SQL or a very very thin wrapper around SQL, that would be my Goldilock solution.
评论 #37119395 未加载
评论 #37119441 未加载
评论 #37121807 未加载
hobbescotchalmost 2 years ago
I’ve been a data engineer for many years and have lots of practice optimizing SQL touching many parts of the language and I still enjoy using SQLAlchemy for its tight, elegant integration with flask&#x2F;django. Of course some queries make sense to optimize with raw sql but I think here, like with many other things, there’s no black&#x2F;white conclusion to draw from these situations.
ggregoirealmost 2 years ago
I&#x27;ve been using PugSQL to write SQL in Python [1].<p>With this package, you write the SQL inside SQL files so you can benefit from syntax highlighting, auto formatting, static analysis, etc. At the difference of writing strings of SQL inside Python files. I&#x27;m surprised this is not more popular.<p>[1] <a href="https:&#x2F;&#x2F;pugsql.org" rel="nofollow noreferrer">https:&#x2F;&#x2F;pugsql.org</a>
评论 #37125452 未加载
megaman821almost 2 years ago
Question to the SQL-only people, how would you handle something dynamic? If I have a database of shoes and want people to be able to find them by brand, size, style, etc., what does that look like?
评论 #37122628 未加载
评论 #37121759 未加载
fb03almost 2 years ago
Alright, let&#x27;s use the custom approach. And then you need another field. and then you need some slight type checking or (de)serialization, which can change over time. You&#x27;ll end up writing your own custom, kludgy ORM over time.<p>I have seen people write their own custom crazy version of GraphQL (&quot;I&#x27;ve created a JSONified way of fetching only some fields from an API call) over ego or just ignorance. It&#x27;s never a good path.<p>Why bother moving away from SQLAlchemy, which will do all of that for you in a simplified, type-checked and portable way? SQLAlchemy is literally one of the best ORMs out there, it&#x27;s ease of use and maintainability is insane.<p>People that complain about ORMs might have never really used SQLAlchemy. It is <i>that</i> good. I&#x27;m a fan and zzzeek is huge force behind why it is so good.<p>And as always, if you need an escape hatch, you can use raw sql for that ONE sql statement that the ORM is giving you grief for.
评论 #37122197 未加载
ak217almost 2 years ago
I&#x27;ve seen many iterations of this type of debate by now, and learned to recognize the patterns. The people arguing for the ostensibly simpler solution are really asking you to trust their ability to architect apps out of simpler building blocks without using an abstraction that they don&#x27;t like. This can work, but it often leads to situations like someone writing a bespoke system, then leaving the job or otherwise imposing extra complexity on the team. In the immediate term, what often gets overlooked is<p>- The ORM is an externally maintained open-source project with a plurality of contributors; &quot;just write SQL&quot; is not<p>- The ORM is designed to support the full lifecycle of the application including migrations; &quot;just write SQL&quot; is not<p>- The ORM is documented to be <i>legible</i> to newcomers; &quot;just write SQL&quot; is not (for all but the simplest of applications)<p>- The ORM is composable and extensible with opinionated and customizable interfaces for doing so (I&#x27;ve lost track of the number of times I&#x27;ve had my mind blown by how elegant and smart Django and SQLAlchemy&#x27;s query management tooling is)<p>- The ORM has a security posture that allows you to both reason about your application&#x27;s security and receive security updates when bugs are found<p>- The ORM is a platform for many other modules responsible for different layers of the application (DRF, OpenAPI, django-admin, testing utilities, etc. etc.) to plug into and allow the application to grow sustainably<p>I now try to guide people to a middle ground. Yes, both Django&#x27;s and SQLAlchemy&#x27;s ORMs can be annoying, have performance issues, etc. But for large applications maintained by multiple people over time, their benefits usually outweigh the drawbacks. Both have extensible architectures that allow customization and opinionated restriction of the interface that the ORM presents. If you&#x27;re unhappy with your organization&#x27;s ORM, I suggest you try that route first.
p4bl0almost 2 years ago
I always write SQL code directly, but that&#x27;s mostly because I actually enjoy writing SQL queries :).
评论 #37121129 未加载
impulser_almost 2 years ago
If you want to try out something cool, check out<p><a href="https:&#x2F;&#x2F;github.com&#x2F;sqlc-dev&#x2F;sqlc">https:&#x2F;&#x2F;github.com&#x2F;sqlc-dev&#x2F;sqlc</a><p>It&#x27;s written in Go and it converts your sql migrations and queries into typesafe code that you use access your database.<p>It currently has a plugin for Python that&#x27;s in Beta, but what essentially does something similar to what this post is saying.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;sqlc-dev&#x2F;sqlc-gen-python">https:&#x2F;&#x2F;github.com&#x2F;sqlc-dev&#x2F;sqlc-gen-python</a><p>You write your migrations, and queries and a config file and it does the rest.
评论 #37128184 未加载
dep_balmost 2 years ago
A good ORM knows when it needs to fuck off. I just want an easy and boiler plate avoiding way to do crud operations on certain tables and map a custom type against a custom query. The lengths I have to go through to just have a custom query in some ORM&#x27;s is mind-boggling. I remember fighting Microsoft&#x27;s Linq to SQL or whatever the incarnation was called so hard. I could do it in the end but it fought me all the way to the end.
Improvotteralmost 2 years ago
It might be worth mentioning LiteralStrings from [PEP 675](<a href="https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0675&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;peps.python.org&#x2F;pep-0675&#x2F;</a>) and how you should use them to prevent SQL injections. I&#x27;m not sure this blog adds much to the discussion when it comes to when to write SQL and when not to. It does not cover the struggles, the benefits, and the downfalls.
waffletoweralmost 2 years ago
As a data engineer, the pattern the OP shares is very familiar. I find it much preferable to use of ORMs for wide variety of reasons. However, I view implementing with SQL as an antiquated problem rather than a pragmatic feature. The evolution of this pattern would be to integrate database querying into languages more directly and eliminate SQL entirely. While this could be achieved in Python, I find that a language like Clojure, via functional programming (FP) primitives and transducers, is a natural candidate, particularly for JVM implemented databases. Rather than encapsulating SQL via query building or ORM based APIs, an FP core could be integrated into database engines to allow, via transducers, complex native forms to be executed directly across database clusters. Apache Spark is an analog of this. In particular the Clojure project, powderkeg (<a href="https:&#x2F;&#x2F;github.com&#x2F;HCADatalab&#x2F;powderkeg">https:&#x2F;&#x2F;github.com&#x2F;HCADatalab&#x2F;powderkeg</a>), as an Apache Spark interface, demonstrates the potential of utilizing transducers in a database cluster context.
atoavalmost 2 years ago
Recently I was wondering myself whether I should just write SQL as I didn&#x27;t particularly enjoy working with SQLAlchemy.<p>Then I discovered peewee. I am happy now.
mkl95almost 2 years ago
A better title would be &quot;just write your own ORM&quot;.<p>I have used several Python ORMs over the years, both for SQL and NoSQL. SQLAlchemy is the most powerful way of interacting with a relational database I have experienced.<p>I also write Go, and when I do, I do not use an ORM. But when it comes to Python I know my solution won&#x27;t be better than SQLAlchemy, so why bother rolling out my own?
sanderjdalmost 2 years ago
I always ctrl-f to search for the word &quot;composability&quot; when I come across arguments like this. I could take or leave ORMs, but relational <i>query-building</i> libraries are invaluable for composability, compared to proliferating mostly-duplicative raw SQL in format strings all over the place.
c120almost 2 years ago
So far all my projects have targeted a specific database with no reason to change it.<p>So what I do is write SQL commands, but keep all inside a specific file or module of the project. So that I can decide later to refactor it into an ORM.<p>I think ORMs are great if you write libraries that target more than one database. Or situations, where you have more than one database and need a proper migration part.<p>If you don&#x27;t need migration, but in the worst case can start with a fresh, empty database, then write SQL.<p>But for production system, the no&#x2F;manual migration might get old quickly. Writing migration code that just adds fields, indexes or tables is easy. But writing code that changes fields or table structures? Not do much.<p>Still, you don&#x27;t need an ORM at the beginning of a project, just don&#x27;t put SQL everywhere.
Dowwiealmost 2 years ago
You&#x27;ll eventually write your own dynamic query building logic if you take this development path
lifewallet_devalmost 2 years ago
I can already see this doesn&#x27;t have connection pooling which all those ORMs he listed have without you knowing what a connection pool is it just works, and scales, doing that on your own is not easy.
phatboyslimalmost 2 years ago
Technologists have a hard time accepting an established standard. Email is a perfect corollary to this conversation. There is a graveyard of companies that have attempted to &quot;Solve email&quot;, yet it is still ubiquitous and attempts to &#x27;improve&#x27; it continue to fizzle out. I&#x27;m not saying that progress, or an attempt at progress, is pointless, but to argue that writing vanilla SQL is somehow antiquated or archaic is false and OP makes several valid points highlighting why it is a perfectly valid approach.
Sparkytealmost 2 years ago
Sentiments on this is that sticking close to native as possible reduces coherency issues between anything. Adding layers of abstraction on top of layers of abstraction often reduces contextual understandings and further dilludes the problem solving technique. If the abstraction is truly needed a thurough way to evaluate executions is needed and a proper way to contextualize which that is not. In-line comments or even very easy to navitage documentation but the former thing or even both is superior to the latter.
ruudaalmost 2 years ago
One challenge working with SQL from statically typed languages (including Python + Mypy) is that you have to convert the query inputs&#x2F;outputs to&#x2F;from types and it&#x27;s a lot of boilerplate. I started an experiment to generate this from annotated queries. [1] Python support is still incomplete, but I&#x27;m using it somewhat successfully for using SQLite from Rust so far.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;ruuda&#x2F;squiller">https:&#x2F;&#x2F;github.com&#x2F;ruuda&#x2F;squiller</a>
评论 #37123600 未加载
sams99almost 2 years ago
For those looking for a rubyish approach to this see: <a href="https:&#x2F;&#x2F;github.com&#x2F;discourse&#x2F;mini_sql">https:&#x2F;&#x2F;github.com&#x2F;discourse&#x2F;mini_sql</a>
stuaxoalmost 2 years ago
Django isn&#x27;t just about the ability to programmatically stick together things to make your query or the migrations. It&#x27;s that, combined with tools that help you debug database issues, and most importantly the patterns that it imposes.<p>As a Django developer it&#x27;s straightforward to go from one Django project to another, which isn&#x27;t the case with other stuff as you don&#x27;t know where everything is going to be.
molly0almost 2 years ago
An ORM makes sense if you need to make very dynamic SQL queries, ie advanced logic at runtime.<p>If your app can work well with static queries then you should not add an ORM.
slotransalmost 2 years ago
Yes. Just write SQL. SQL is good.<p>Never use an (active record) ORM. Ever. For any purpose. They are a disastrous idea that should be un-invented.<p>I have this discussion with a lot of people who claim they cannot imagine working without an ORM and that &quot;surely&quot; using SQL is so much more work blah blah blah. Yet they have never tried! And aren&#x27;t willing to try!<p>You should try it.
hprotagonistalmost 2 years ago
If you haven’t yet, check out <a href="https:&#x2F;&#x2F;pugsql.org&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;pugsql.org&#x2F;</a> . all the power of sqla-core, none of the ORM fuss.<p><pre><code> PugSQL is a simple Python interface for using parameterized SQL, in files, with any SQLAlchemy-supported database.</code></pre>
pharmakomalmost 2 years ago
I no longer feel the need for an ORM. Here&#x27;s what I do instead:<p>- immutable record type for each table in the database (could be a data class)<p>- functions for manipulating the tables that accept or return the immutable record types and directly use SQL<p>- that&#x27;s it<p>you can generate the functions from the database schema if its too much boilerplate.
timmitalmost 2 years ago
Based on my personal experience, I have seen some raw SQL codes about 200 to 1000 lines in some production source codes,<p>not readable at all, not easy to change, which is a terrible development experience.<p>I guess if it is simple CRUD, it does not give too much problem, but it will definitely work in a complex case.
jredwardsalmost 2 years ago
There&#x27;s a huge module in our python codebase that approaches building queries in roughly raw SQL. Let me tell you, tracking how data moves from one stage to the next in that &quot;ETL pipeline&quot; is an absolute nightmare. Never again.
metalforeveralmost 2 years ago
What happens at big companies is that they will build a custom ORM over time , and it will be way shittier and more vulnerable than if you had just used one in the first place.
semrekkersalmost 2 years ago
Shameless plug, with channel support: <a href="https:&#x2F;&#x2F;github.com&#x2F;semrekkers&#x2F;sqlz">https:&#x2F;&#x2F;github.com&#x2F;semrekkers&#x2F;sqlz</a>
never_inlinealmost 2 years ago
The premise is that Go language users only use standard library SQL package.<p>Anecdotally, I haven&#x27;t seen a place where Go is used without something like gorm or sqlc.
评论 #37121528 未加载
CodeWriter23almost 2 years ago
SQL and ORM both have merit in different situations. Pick the correct tool for the given use case, and don’t be afraid to mix &amp; match IMO.
bastardoperatoralmost 2 years ago
No thanks, been there, done that. Writing SQL by hand almost never scales.
izoowalmost 2 years ago
To those who write plain SQL in Python, what do you use for migrations?
评论 #37124791 未加载
sakexalmost 2 years ago
C++: Just write Assembly
bafealmost 2 years ago
Just write SQL and eventually you will reinvent 50% of any ORM
felipetrzalmost 2 years ago
&quot;without using ORMs&quot;<p>...<p>Proceeds to create an ad-hoc ORM.
评论 #37121226 未加载
ploppyploppyalmost 2 years ago
Low quality naive summary.
评论 #37121254 未加载