python - getting the id of a created record in SQLAlchemy -


How do I get a record ID created in SQLAlchemy?

I am doing:

  enter the engine.execute ("User values ​​(1, 'John')"  
< / Div>

When you execute plain text statements, then you are at the mercy of the DBAPI that you are using as Whether or not the new PK value is available and what it means. You will take it as a result with SQLite and MySQL DBAPI. Stolide, which gives you the value of .lastrowid for the cursor. With PG, Oracle, etc., there is no ".lastrowid" - as someone else has said that you can use "returning" for those people, resulting in result.fetchone () (though the oracle's Using Returning, is not taking advantage of again to produce SQLite expression, many weird steps are required), or if returning is not available, then you can directly access sequence access (NEXTVAL in PG), or "POS Fetch can use "operation (PR Krurvil, @@ identity or Scop_ identity ()) are MSSQL).

Does the Correct Correct? This is the reason that you are better off using table.insert () . The primary system of SQLLame to provide new generation PK is designed to work with these structures. The one you are there, the results. Last_inserted_ids () method gives you a newly generated (possibly composite) PK in all cases regardless of the backend. The above methods of .lastrowid, sequence execution, returning etc. are all for you (0.6 uses returning if available).


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -