You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by Graham Leggett via torque-dev <to...@db.apache.org> on 2026/06/15 21:54:08 UTC
Torque7 and postgresql autoincrementing primary keys not working
Hi all,
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.
The generated code looks like this:
public ColumnValues buildColumnValues(Type type)
throws TorqueException
{
ColumnValues columnValues = new ColumnValues();
if (!type.isNew()
|| type.getTypeId() != 0)
{
columnValues.put(
TypePeer.TYPE_ID,
new JdbcTypedValue(
type.getTypeId(),
4));
}
columnValues.put(
TypePeer.SERIAL,
new JdbcTypedValue(
type.getSerial(),
12));
columnValues.put(
TypePeer.NAME,
new JdbcTypedValue(
type.getName(),
12));
columnValues.put(
TypePeer.DESCRIPTION,
new JdbcTypedValue(
type.getDescription(),
12));
columnValues.put(
TypePeer.SIGNATURE_ID,
new JdbcTypedValue(
type.getSignatureId(),
4));
return columnValues;
}
When the object isNew(), and the type_id is the default value of 0, it is expected that the sequence mechanism kicks in and uses the postgresql sequence to populate the field.
This is missing from the above code, type_id isn't set at all, and so the insert fails as expected with a NOT NULL constraint.
This corresponds to this template, which doesn't show anything related to auto increment:
http://svn.apache.org/repos/asf/db/torque/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildColumnValues.vm
I am assuming there is a step before that calls this:
https://svn.apache.org/repos/asf/db/torque/trunk/torque-runtime/src/main/java/org/apache/torque/adapter/PostgresAdapter.java
/**
* @param name The name of the field (should be of type
* <code>String</code>).
* @return SQL to retreive the next database key.
* @see org.apache.torque.adapter.Adapter#getIDMethodSQL(Object)
*/
@Override
public String getIDMethodSQL(Object name)
{
return ("select nextval('" + name + "')");
}
But stepping through in the debugger doesn't take me anywhere near the code.
The table is defined like this:
<table name="type"
description="Company Types"
idMethod="native"
interface="com.example.Type">
<column
name="type_id"
required="true"
primaryKey="true"
autoIncrement="true"
type="INTEGER"
javaType="primitive"
description="Type Id"/>
<column
name="serial"
required="true"
type="VARCHAR"
description="Serial Number"/>
<column
name="name"
required="true"
type="VARCHAR"
description="Name of the type"/>
<column
name="description"
required="true"
type="VARCHAR"
description="Friendly description of the type"/>
<column
name="signature_id"
required="true"
type="INTEGER"
description="Digital signature locking this row"/>
<foreign-key foreignTable="signature" onUpdate="cascade" onDelete="cascade">
<reference
local="signature_id"
foreign="signature_id"/>
</foreign-key>
<unique name="type_unique">
<unique-column name="serial"/>
</unique>
<index name="type_index">
<index-column name="serial"/>
</index>
</table>
Is idMethod="native" correct or should this be something else?
Regards,
Graham
--
AW: Torque7 and postgresql autoincrementing primary keys not working
Posted by "Kallidis, Georg" <ge...@fu-berlin.de>.
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 <to...@db.apache.org>
Gesendet: Dienstag, 16. Juni 2026 00:17
An: Apache Torque Developers List <to...@db.apache.org>
Cc: Graham Leggett <mi...@sharp.fm>
Betreff: Re: Torque7 and postgresql autoincrementing primary keys not working
On 15 Jun 2026, at 22:54, Graham Leggett <mi...@sharp.fm> 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
--
Re: Torque7 and postgresql autoincrementing primary keys not working
Posted by Graham Leggett via torque-dev <to...@db.apache.org>.
On 15 Jun 2026, at 22:54, Graham Leggett <mi...@sharp.fm> 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
--