Marc-Andre Lemburg <m...@egenix.com> added the comment:
On 04.01.2022 21:02, Erlend E. Aasland wrote: > > Erlend E. Aasland <erlend.aasl...@innova.no> added the comment: > >> If possible, it's usually better to have the .executemany() create a >> cursor with an output array providing the row ids, e.g. using "INSERT ... >> RETURNING ..." (PostgreSQL). That way you can access all row ids and >> can also provide the needed detail in case the INSERTs happen out of >> order to map them to the input data. > > Hm, maybe so. But it will add a lot of overhead and complexity to > executemany(), and there haven't been requests for this feature for sqlite3. > AFAIK, there hasn't been request for lastrowid for executemany() at all. > OTOH, my proposal of modifying lastrowid to always show the rowid of the > actual last inserted row is a very cheap operation, _and_ it simplifies the > code (=> increased maintainability), so I think I'll go for that. Sorry, I wasn't suggesting this for SQLite; it's just a better and more flexible option than using cursor.lastrowid where available. Sadly, the PG extension is not standards conform SQL. >> For cases where you don't need sequence IDs, it's often better to >> not rely on auto-increment columns for IDs, but instead use random >> pre-generated IDs. Saves roundtrips to the database and works nicely >> with cluster databases as well. > > Yes, but in those cases you keep track of the row id yourself, so you > probably won't need lastrowid ;) Indeed, and that's the point :-) Those auto-increment ID fields are not really such a good idea to begin with. Either you know that you will need to manipulate the rows after inserting them (in which case you can set an ID) or you don't care about the individual rows and only want to aggregate or search in them based on other fields. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue46249> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com