0

I have a problem with PK generation strategy in cayenne.

I'm using PostgreSQL 9.6 with Apache Cayenne 4.0.B1.

This is my table in postgres:

create table ui_template (
  id varchar(50) primary key default uuid_generate_v1(),
  path varchar(300) not null,
  type varchar(50) not null
);

Then I'm doing Tools -> Reengineer Database Schema in Cayenne Modeler. I got table and enity. And now i have to set PK Generation Strategy to Database-Generated, to let DB generate my PK's. Everything works fine when I'm inserting objects through ObjectContext. But, if I'm running gradle task cdbimport it overwrites my datamap.map.xml file with another one without Generation Strategy. Please give me advice what I'm doing wrong.

This is my gradle task

buildscript {
    // add Maven Central repository
    repositories {
        mavenCentral()
    }
    // add Cayenne Gradle Plugin
    dependencies {
        classpath group: 'org.apache.cayenne.plugins', name: 'cayenne-gradle-plugin', version: '4.0.B1'
        classpath group: 'org.postgresql', name: 'postgresql', version: '42.1.3'

    }
}
// apply plugin
apply plugin: 'org.apache.cayenne'

// set default DataMap
cayenne.defaultDataMap 'resources/datamap.map.xml'

// add Cayenne dependencies to your project
dependencies {
    // this is a shortcut for 'org.apache.cayenne:cayenne-server:VERSION_OF_PLUGIN'
    compile cayenne.dependency('server')
    compile cayenne.dependency('java8')
}


cdbimport {
    dataSource {
        driver 'org.postgresql.Driver'
        url 'jdbc:postgresql://localhost:5432/my_db'
        username 'user'
        password 'pass'
    }
}
Anton Lee
  • 684
  • 4
  • 10

1 Answers1

1

You are doing nothing wrong, it seems that you found a weak spot in cdbimport. In your case Cayenne doesn't understand default values, for Postgres DB Cayenne marks only serial columns as db-generated.

You can either change your PK to serial (unless you have some special requirements it is always a good idea) or you can try to exclude those PKs from cdbimport task like this:

cdbimport {
    //...
    includeTable 'ui_template', {
        excludeColumns 'id'
    }
}

Be aware that excluding PKs can lead to problems with importing relationships. And moreover if you use includeTable you should explicitly list all required tables.

Nikita
  • 266
  • 2
  • 7
  • Thanks, i will think about serial keys, but inside our project everywere uuid as PK, and I want to map cayenne to exists DB. – Anton Lee Aug 03 '17 at 05:40