3 ms·
> Why are list(Table.objects.filter(id=?)) and Table.objects.filter(id=?).update(thing=?) better? I'm not familiar with SQLAlchemy, but with other ORMs, this i
by notJim 5y ago
> Why are list(Table.objects.filter(id=?)) and Table.objects.filter(id=?).update(thing=?) better?
I'm not familiar with SQLAlchemy, but with other ORMs, this isn't how you'd write this.
It would be more like Widgets.findOne(id) and then widget.property = bla; widget.save();
For me this is much more natural than writing SQL, especially for the update query.
There are usually also query helpers for simple queries by fields, like Table.findWhere({property: 'value', property2: 'value2'}).
To me this syntax is much less awkward than writing SQL in strings, and it saves you from repeatedly writing out the SELECT statements and where clauses.
- OJFord 5y agoThat was Django, not SQLAlchemy. You can also do it more as you describe, `get(pk=id)` instead of `findOne`. I just find it pointlessly different to SQL, not really saying it's worse. The SQL would be `update table set thing = ? where id = ?`.