kaleidos / grails-postgresql-extensions

Grails plugin to use postgresql native elements such as arrays, hstores,...
Apache License 2.0
78 stars 63 forks source link

Create sequence before table #100

Open make opened 7 years ago

make commented 7 years ago

I am using following domain class mapping to enable direct SQL inserts.

class Foo {
    static mapping = {
        id defaultValue:"nextval('seq_foo')"
    }
}

When using dbCreate = 'update' in datasource table and sequence are automatically created when running 'grails run-app'. The problem is that table is created first and causes error:

[main] ERROR o.h.tool.hbm2ddl.SchemaUpdate - ERROR: relation "seq_foo" does not exist

The workaround is to rerun "grails run-app".

Sequence should be created before table to fix that issue.

ilopmar commented 7 years ago

Maybe I'm missing something but why are you doing this? When you say to enable direct SQL inserts you mean to be able to do an insert without setting a value for the id column?

make commented 7 years ago

Yes. I want to enable both manual SQL inserts and GORM inserts using the same auto-incrementing id sequence.

So mapping id defaultValue:"nextval('seq_foo')" should be declared to enable following to work

new Sql(dataSource).executeInsert('INSERT INTO foo DEFAULT VALUES')

Equal GORM insert is

new Foo().save(flush:true)