Grails生成

时间:2013-05-06 15:42:16

标签: hibernate postgresql grails

我正忙于在Grails项目上从MySQL到Postgres进行相当复杂的数据迁移。

我正在使用GORM在PostGres中生成模式,然后使用MySQL - > mysqldump,一些数据操作,然后导入Postgres。

显然,从MySQL到PSQL的每个人都认为理所当然的是id列上的auto_inc。因此,我已将以下映射添加到每个域对象(根据各种站点和指南说明),这应该管理id列的所有排序:

static mapping = {
        id generator: 'sequence'
}

但是,在执行\ d +帐户时,我会看到以下行:

public.product
----------------------+-----------------------------+-----------+----------+-
 id                   | bigint                      | not null  | plain    | 

......这与我期望的有些不同(我希望它是连续的或大的)。因此,无论如何,我迁移数据,然后启动应用程序,然后通过/ product / create操作添加产品来测试它。没有骰子,我可以看到一个hibernate异常弹出,抱怨唯一键(insert语句试图使用id的2.我刷新页面几次(产生更多失败,但仍然增加了nextval)最终它达到142(表中当前有141个产品),然后它成功了。所以显然我必须更新表中使用的每个id序列的nextval。可悲的是,我不能真的告诉wtf Grails使用的是序列,因为它没有被表引用,DB中也没有任何新表。

我已经阅读了(How to reset postgres' primary key sequence when it falls out of sync?)的提示,但由于主键上没有序列号,所以这并不合适。

我可以使用min_lo值(如果sequence方法甚至认为非hilo策略的下限),但理想情况下我想要一些东西我可以运行一些SQL来修复迁移后

干杯。

编辑:PostGreSQL 9.1,Grails 2.0.3(是的,这将在迁移后更新)。

1 个答案:

答案 0 :(得分:1)

请参阅此文档。您必须在启动应用程序之前更新序列。

http://www.postgresql.org/docs/9.1/static/functions-sequence.html

currval(regclass)                   bigint  Return value most recently obtained with nextval for specified sequence
lastval()                           bigint  Return value most recently obtained with nextval for any sequence
nextval(regclass)                   bigint  Advance sequence and return new value
setval(regclass, bigint)            bigint  Set sequence's current value
setval(regclass, bigint, boolean)   bigint  Set sequence's current value and is_called flag