I'm upgrading to v4 so it's time to address some things I worked around in v3.The first is shown in the attached stack overflow question. Our postgres tables have audit triggers which can influence the value returned by lastval().The best way to handle getting the new id is to add "RETURNING id", where "id" is the name of the key column, to the end of the insert. But this will require access to the model to get the correct column name (account_id, order_id, product_id). CURRVAL() is another alternative but also requires model information.Any advice on what we could do?In v3, I brought in the servicestack auth services and customized with this function. public static TReturn GetLastId<TModel, TReturn>(this IDbConnection db) { var table = OrmLiteConfig.DialectProvider.NamingStrategy.GetTableName(ModelDefinition<TModel>.Definition.ModelName); var column = OrmLiteConfig.DialectProvider.NamingStrategy.GetColumnName(ModelDefinition<TModel>.Definition.PrimaryKey.FieldName); const string sql = "SELECT currval(pg_get_serial_sequence(@table,@column))"; var id = db.QueryScalar<TReturn>(sql, new {table, column}); return id; }

mythz
—
2015-03-11T11:48:49Z —
#2

You can maybe change what command is used to retrieve the last insert id with something like:

PostgreSqlDialect.SelectIdentitySql = "return id";

This would override the default "SELECT LASTVAL()" sql used in PostgreSql, so you may want to wrap it in an extension method that sets it back.

ArchiveUser
—
2015-03-11T11:48:49Z —
#3

Jeff Gabhart:

The default base provider will put a semicolon between the insert and the select identity sql which won't work out.BUTI see that I can override InsertAndGetLastInsertId<T>(IDbCommand dbCmd) too and that has T so I can access the model definition. Might be getting somewhere..

ArchiveUser
—
2015-03-11T11:48:50Z —
#4

Jeff Gabhart:

My v4 upgrade in July was a trial run, and now I'm upgrading for real.Here is what I ended up using.

In our case, these methods for getting the generated Id are more reliable due to audit triggers on our tables. On an insert, a trigger fires which also inserts an audit row in a different table. The problem is that LASTVAL() will return the Id from the audit table and not the one you are actually interested in.

If you think this should be included in the default Postgres dialect, I can create a pull request.