gpt4 book ai didi

java - 每次启动应用程序时,Spring data.sql 文件都会添加一条新记录。

转载 作者:行者123 更新时间:2023-11-29 13:14:06 26 4
gpt4 key购买 nike

这是我的 application.properties 文件:

spring.datasource.url= jdbc:postgresql://localhost:5432/Crypto
spring.datasource.username=postgres
spring.datasource.password=wololo
spring.jpa.hibernate.ddl-auto=update
spring.datasource.initialization-mode=always

这是我的 data.sql 文件:

INSERT INTO crypto(departure, details, lowest_price_date, lowest_price_ever, coin_name, url)
VALUES (null, null, null, 10, 'foooobs', 'http://foo.foo');

问题是每次我重新启动应用程序时,都会添加一条新记录,即使它存在于上一个 session 中也是如此。我只想用数据初始化数据库一次并且永远不更改它。

有什么想法吗?

最佳答案

一个可能的解决方案是使用固定的 ids :

INSERT INTO 
crypto (id, departure, details, lowest_price_date, lowest_price_ever, coin_name, url)
^^
VALUES (1, null, null, null, 10, 'foooobs', 'http://foo.foo');
^

那么会发生什么,首先你正在用这个解决方案制作spring.jpa.hibernate.ddl-auto=update,如果记录没有退出然后创建一个新的,如果它退出,然后只更新属性,而不创建新记录。


或者您可以看看 flyway ,在这种情况下它是一个很好的工具。

关于java - 每次启动应用程序时,Spring data.sql 文件都会添加一条新记录。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51312837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com