Hi Graham,

I understand Torque generates, if a column has autoIncrement="true" for target 
database Postgresql the sequence, but not the required entry in the generated 
SQL in the column.

1. The method  getAutoIncrement in 
org.apache.torque.templates.platform.PlatformPostgresqlImpl returns an empty 
String instead of nextval('" + name + "')  (Postgrsql8)

2. in PostgresAdapter the method getIDMethodSQL  is not called, as for the 
table idMethod="native", you might try to change to idMethod="sequence".
Then it returns ("select nextval('" + name + "')"); which is not right, what we 
want, but read on 😊

3. This is, where the "hack" is documented, and which explains why 
getAutoIncrement returns nothing:

In org.apache.torque.util.BasePeerImpl, line 597ff:

doInsert(Column[], Criteria, String, Connection)
if (keyGen instanceof SequenceIdGenerator)
                {
                    SequenceIdGenerator sequenceIdGenerator
                    = (SequenceIdGenerator) keyGen;
                    String idSql = sequenceIdGenerator.getIdSql(
                            getIdMethodInfo());
                    // This is a bit of a hack.
                    // The idSql is usually a stand-alone statement, but we
                    // need the part to be inserted as a column.
                    // Therefore we extract the complete word containing
                    // the term nextval

Fast checking svn this is already in the code since 2013 (TORQUE-305 fix )..

Having said that, it might be an idea to use the much simpler keyword serial 
which is supported since Postgresql9 or even Postgrsql10 syntax ?

Best regards, Georg


-----Ursprüngliche Nachricht-----
Von: Graham Leggett via torque-dev <[email protected]> 
Gesendet: Dienstag, 16. Juni 2026 00:17
An: Apache Torque Developers List <[email protected]>
Cc: Graham Leggett <[email protected]>
Betreff: Re: Torque7 and postgresql autoincrementing primary keys not working

On 15 Jun 2026, at 22:54, Graham Leggett <[email protected]> wrote:

> After upgrading from torque3.3 to torque7 (big jump), I have everything 
> compiling, but what has suddenly vanished are autoincrementing database 
> primary keys for postgres.

Stepping through more code, I get here:

    public ObjectKey<?> doInsert(
            final ColumnValues insertValues,
            final Connection connection)
                    throws TorqueException
    {
        if (insertValues == null)
        {
            throw new TorqueException("insertValues is null");
        }
        if (connection == null)
        {
            throw new TorqueException("connection is null");
        }
        String databaseNameFromInsertValues = insertValues.getDbName();
        if (databaseNameFromInsertValues == null)
        {
            databaseNameFromInsertValues = getDatabaseName();
        }
        Database database = Torque.getDatabase(databaseNameFromInsertValues);
        Object keyInfo = getIdMethodInfo();
        IdGenerator keyGen = database.getIdGenerator(
                getTableMap().getPrimaryKeyMethod());

        SimpleKey<?> id = null;
        // can currently generate only single column pks, therefore a single
        // columnMap is ok
        ColumnMap primaryKey = null;
        if (keyGen != null)
        {
            // fail on multiple pks
            primaryKey = getTableMap().getPrimaryKey();

            // primaryKey will be null if there is no primary key
            // defined for the table we're inserting into.
            if (keyGen.isPriorToInsert() && primaryKey != null
                    && !insertValues.containsKey(primaryKey))
            {
                id = getId(primaryKey, keyGen, connection, keyInfo);
                insertValues.put(
                        primaryKey,
                        new JdbcTypedValue(id.getValue(), id.getJdbcType()));
            }
        }


The above code appears to have picked up the 
org.apache.torque.adapter.HsqldbAdapter based on the JDBC driver in the unit 
tests, instead of postgres as defined in the maven torque plugin:

                <torque.targetDatabase>postgresql</torque.targetDatabase>

hsqldb was previously in postgres-mode using sql.syntax_pgs=true; - does that 
no longer work?

Regards,
Graham
--


Reply via email to