How to insert a specific UUID in h2 database?

我的未来我决定 提交于 2019-12-11 13:21:34

问题


I need to insert some default data in my database. I am using Spring Boot with the Flyway integration. For testing, I use H2. For production MySQL will be used.

I have made separate Flyway migration scripts so I can use database specific stuff for the default data (Creating the tables is done in a common script).

For MySQL, I have something like this:

INSERT INTO survey_definition (id, name, period) 
VALUES (0x2D1EBC5B7D2741979CF0E84451C5BBB1, 'disease-activity', 'P1M');

How can I do the same for H2?

I only found the RANDOM_UUID() function, which works, but I need use a known UUID because I am using it as foreign keys in further statements.


回答1:


Best if you use a syntax that works for all databases. I think most databases don't support the 0x syntax. For H2, this would work:

INSERT INTO survey_definition (id, name, period) 
VALUES ('2D1EBC5B7D2741979CF0E84451C5BBB1', 'disease-activity', 'P1M');

But to get a cross-database syntax, you may need to create a user defined function (for example uuid) and then use:

INSERT INTO survey_definition (id, name, period) 
VALUES (uuid('2D1EBC5B7D2741979CF0E84451C5BBB1'), 'disease-activity', 'P1M');


来源:https://stackoverflow.com/questions/36494510/how-to-insert-a-specific-uuid-in-h2-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!