Aura.SqlMapper_Bundle icon indicating copy to clipboard operation
Aura.SqlMapper_Bundle copied to clipboard

Postgres insert with sequence primary key

Open cxj opened this issue 8 years ago • 0 comments

I need to insert a row through my Mapper and Row Gateway and get back the primary key value for the newly created row. This is a common database problem. Postgres has a couple of somewhat unique features which make doing this in Postgres somewhat different than in MySQL.

Aura\SqlQuery supports two of them (return of last insert ID via automatic Postgres sequence name generation) and the SQL RETURNING clause on INSERTs.

The former (last insert ID) is also supported by Aura\SqlMapper_Bundle, but fails to work in all cases, because of the assumption it makes about sequence names for primary keys (i.e. table_column_seq). Unfortunately, at my place of employ, we have hundreds of tables and paired primary key sequences which follow a company-standard naming scheme different from Aura's assumption.

The RETURNING clause would thus work perfectly for my application ... but! There does not appear to be support for it at the Mapper / Row Gateway level of code, although it appears well-supported at the Aura\SqlQuery level.

I'd like to see support for the RETURNING clause in SqlMapper_Bundle. I'm willing to write the code, tests and documentation. But I'd like a little guidance on the proper Aura way to do it, and maybe some agreement on it being a good idea. Or explanation of why it's a bad idea. :-)

cxj avatar Jul 28 '15 20:07 cxj