Python-peewee get last saved row
orm, peewee, python
Solution
User.select().order_by(User.id.desc()).get()
This will get the last-created user assuming the IDs is an auto-incrementing integer (the default).
Or if you want to get e.g. last created customer (user), you can write it like this:
User.select().where(User.type == "customer").order_by(User.id.desc()).get()
If you want to get the last saved user, you need to add a timestamp to indicate when the user is saved.
Update:
Peewee also now supports `RETURNING` clause for Postgres database. You can add a `RETURNING` clause to any `INSERT`, `UPDATE` or `DELETE` query. Check out the docs:
- http://docs.peewee-orm.com/en/latest/peewee/querying.html#returning-clause
- http://docs.peewee-orm.com/en/latest/peewee/api.html?highlight=returning#InsertQuery.returning
Problem
is there a way how to obtain last saved row in database while using peewee with all its attributes? Let's say I do this: ``` user = User.create( email = request.json['email'], nickname = request.json['nickname'], password = request.json['password'], salt = "salt" ) ``` But `user.id` is `None` and the only attributes I can get are those specified above. I could call `select()` method but isn't there there any faster way? Thanks