datanucleus / datanucleus-rdbms

DataNucleus support for persistence to RDBMS Datastores
30 stars 67 forks source link

PostgreSQL: Use BIGSERIAL when identity column is mapped to BIGINT #456

Closed nscuro closed 2 years ago

nscuro commented 2 years ago

The identity keyword for PostgreSQL was hardcoded to SERIAL, which lead to gnarly inconsistencies like this:

class User {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
    private long id;

    // ...
}
CREATE TABLE "USER"
(
    "ID" SERIAL -- Wrong: SERIAL is equivalent to integer / int4. Java type is long, so this should be BIGSERIAL
    -- ...
);

CREATE TABLE "USERS_PERMISSIONS"
(
    "USER_ID" int8 NOT NULL -- Correct: int8 aliases bigint
    -- ...
);

To solve this I needed additional context about the mapped column type in DatastoreAdapter#getIdentityKeyword. Added a new default method to the DatastoreAdapter interface, that just calls the original getIdentityKeyword(StoreManager) method if not overridden, ensuring that this change is backward compatible.

Fixes #455

Signed-off-by: nscuro nscuro@protonmail.com