• 0 Posts
  • 11 Comments
Joined 2 months ago
cake
Cake day: August 10th, 2026

help-circle






  • It’s tricky because we’re not used to write real SQL when using query builders

    Speak for yourself. I quite frequently write ad-hoc queries (yes writing the queries, not asking a fucking robot in 8 iterations of increasingly verbose detail to write it for me). That works perfectly well because I know what I want at the time I’m writing the query.

    That isn’t at all the same thing as using a query builder to allow the application to be able to filter on arbitrary columns, or complex value types, or have conditional conditions, or conditional columns returned, or any number of other dynamic aspects that are literally the reason for query builders to exist.

    as soon as you learn how to do what you did with a builder with pure SQL, well it becomes trivial.

    Ok fuck it, I’ll bite. What’s the “trivial, pure SQL” to take a variable length array/list/set/tuple/<whatever structure your language uses for a bunch of values>, and check if a column is equal to one of them.

    i.e convert ['foo', 'bar', 'baz'] into ... WHERE col IN ('foo', 'bar', 'baz');.


    score 1 · canonical




  • Person.objects.filter(gender_concept_id=8507).order_by(‘year_of_birth’).values_list(‘person_id’, flat=True)[:100]

    The main advantages I find this has are that filter is just expressed with keyword arguments

    I don’t use Django (and vary little Python) so I had to look this up, but the “advantage” you’re referring to kind of goes down the toilet once you need anything other than the default “equals” operator, IMO.

    This is the first example I found from the Django docs, for the filter method:

    Entry.objects.filter(pub_date_year=2006)
    

    Oh wait, no sorry it was actually this:

    Entry.objects.filter(pub_date__year=2006)
    

    Which kind of demonstrates my point. The two are not particularly distinctive visually, but they are obviously very different in terms of the SQL that is generated.

    Using variadic arguments to define the column of a predicate is interesting, but the part where you chain together a bunch of double underscores and append them to the column name, honestly sounds like the kind of weird shit Wordpress or some other early-2000’s PHP monstrosity would do (and I say that as someone who makes a significant amount of my income from PHP-related projects).


    score 1 · canonical