3 ms·
> ORMs can't use DB specific features This is technically false. There is no technical reason an ORM couldn't use DB-specific features. It's also practically f
by develatio 3y ago
> ORMs can't use DB specific features
This is technically false. There is no technical reason an ORM couldn't use DB-specific features. It's also practically false. Have a look at https://docs.djangoproject.com/en/4.2/ref/databases/ https://docs.djangoproject.com/en/4.2/ref/databases/
Django's ORM will do quite clever stuff, specific to your database (eg, if you're using Postgres, it will create additional indexes using appropriate PSQL operator classes).
> I can insert an entry and return the generated ID in a single query. ORMs can't do that
This is also false. Calling .save() on a model will issue an INSERT statement, then the ORM will assign the auto-incremented PK to the "id" property of the model without issuing any further queries. https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#auto-incrementing-primary-keys https://docs.djangoproject.com/en/dev/ref/models/instances/?...
> I can also upsert entries in a single statement. ORMs can't do that.
This is also technically and practically false. There is no technical reason an ORM couldn't do an upsert with a single query (granted that the database itself supports it). Practically, while Django's ORM itself can't do this, you could easily use django-postgres-extras (which is just an extension to the ORM's capabilities) to issue an "ON CONFLICT target", which will achieve an upsert with a single query: https://django-postgres-extra.readthedocs.io/en/master/conflict_handling.html https://django-postgres-extra.readthedocs.io/en/master/confl...