postgreSql中使用modal层save自增失败, 已解决

org.postgresql.util.PSQLException: 错误: null value in column "id" of relation "stock" violates not-null constraint

pgsql已经设置了自增字段,求大佬指点 @jfinal

根据波总QQ里面耐心指导:

说白了,还是因为你的  id 为 null ,保存的时候用不上自增
null 值它也是个值, 所以你不能给它赋值
你这样,在 save() 之前,调用一下   model.removeNullValueAttrs() 方法名我忘了,你敲一下 remove...

你还可以  model.remove("id")  只移除 id 这个字段, 保留其它字段的 null 值,如果有需要的话

acc.remove("id").save();
写在一行,表明这个是需要自增的,是一体的


Sql = insert into "account"("salt", "billing_company_id", "user_name", "pass_word", "created", "nick_name", "shortcut_key", "state", "id", "avatar", "updated") values('8lhqfxaD1z2D8Z1GGQyw97ozkRWXW069', '13212122312', '61af3290d6ea644f990694ea7b169e7615b2265435766ed72e18ce465262c415', '2022-09-20 09:05:49.819', 'test', '', '1', 'x.jpg', '2022-09-20 09:05:49.819', ?, ?)

2022-09-20 09:05:49
[ERROR]-[Thread: XNIO-2 task-1]-[com.jfinal.core.ActionHandler.handle()]: com.jfinal.admin.account.AccountAdminController.saveVue() : /admin/account/saveVue
com.jfinal.plugin.activerecord.ActiveRecordException: org.postgresql.util.PSQLException: 错误: null value in column "id" of relation "account" violates not-null constraint
  详细:失败, 行包含(null, test, 13212122312, 61af3290d6ea644f990694ea7b169e7615b2265435766ed72e18ce465262c415, 8lhqfxaD1z2D8Z1GGQyw97ozkRWXW069, 1, x.jpg, 2022-09-20 09:05:49.819+08, 2022-09-20 09:05:49.819+08, , t, null).
	at com.jfinal.plugin.activerecord.Model.save(Model.java:610)
	at com.jfinal.admin.account.AccountAdminService.save(AccountAdminService.java:160)
	at com.jfinal.admin.account.AccountAdminController.saveVue(AccountAdminController.java:138)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:97)
	at com.jfinal.admin.auth.AdminAuthInterceptor.intercept(AdminAuthInterceptor.java:50)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.admin.auth.IsInitialPwInterceptor.intercept(IsInitialPwInterceptor.java:17)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.admin.common.CommonLogInterceptor.intercept(CommonLogInterceptor.java:45)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.admin.login.LoginSessionInterceptor.intercept(LoginSessionInterceptor.java:59)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.core.ActionHandler.handle(ActionHandler.java:110)
	at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:90)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:275)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:79)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:134)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:131)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:255)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:79)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:100)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:387)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:852)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2019)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1558)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1423)
	at org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
	at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: org.postgresql.util.PSQLException: 错误: null value in column "id" of relation "account" violates not-null constraint
  详细:失败, 行包含(null, test, 13212122312, 61af3290d6ea644f990694ea7b169e7615b2265435766ed72e18ce465262c415, 8lhqfxaD1z2D8Z1GGQyw97ozkRWXW069, 1, x.jpg, 2022-09-20 09:05:49.819+08, 2022-09-20 09:05:49.819+08, , t, null).
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2676)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2366)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:356)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:496)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:413)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:190)
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:152)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:3253)
	at com.alibaba.druid.filter.FilterAdapter.preparedStatement_executeUpdate(FilterAdapter.java:1092)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:3251)
	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.executeUpdate(PreparedStatementProxyImpl.java:194)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:255)
	at com.jfinal.plugin.activerecord.Model.save(Model.java:605)
	... 53 more

QQ图片20220920000507 (1).png


https://jfinal.com/doc/5-12

尝试过oracle的方式,还是不行:

2.png

Sql = insert into "account"("salt", "billing_company_id", "user_name", "pass_word", "created", "nick_name", "shortcut_key", "state", "id", "avatar", "updated") values('mylSEOQU1dq2H036YPgLdQPGpJgG4OwI', '13212122312', '6f7395b0497905cb78f9223ca15d659720a1d4ad2c9e16a860c99b653d294537', '2022-09-20 09:04:25.569', 'test', '', '1', 'account_id_seq.nextval', 'x.jpg', '2022-09-20 09:04:25.569', ?)

2022-09-20 09:04:25
[ERROR]-[Thread: XNIO-1 task-4]-[com.jfinal.core.ActionHandler.handle()]: com.jfinal.admin.account.AccountAdminController.saveVue() : /admin/account/saveVue
com.jfinal.plugin.activerecord.ActiveRecordException: org.postgresql.util.PSQLException: 错误: 无效的类型 integer 输入语法: "account_id_seq.nextval"
  在位置:unnamed portal parameter $9 = '...'
	at com.jfinal.plugin.activerecord.Model.save(Model.java:610)
	at com.jfinal.admin.account.AccountAdminService.save(AccountAdminService.java:160)
	at com.jfinal.admin.account.AccountAdminController.saveVue(AccountAdminController.java:138)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:97)
	at com.jfinal.admin.auth.AdminAuthInterceptor.intercept(AdminAuthInterceptor.java:50)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.admin.auth.IsInitialPwInterceptor.intercept(IsInitialPwInterceptor.java:17)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.admin.common.CommonLogInterceptor.intercept(CommonLogInterceptor.java:45)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.admin.login.LoginSessionInterceptor.intercept(LoginSessionInterceptor.java:59)
	at com.jfinal.aop.Invocation.invoke(Invocation.java:91)
	at com.jfinal.core.ActionHandler.handle(ActionHandler.java:110)
	at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:90)
	at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
	at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
	at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
	at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
	at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
	at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
	at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
	at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117)
	at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
	at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
	at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
	at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
	at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
	at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
	at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
	at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:275)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:79)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:134)
	at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:131)
	at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
	at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
	at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:255)
	at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:79)
	at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:100)
	at io.undertow.server.Connectors.executeRootHandler(Connectors.java:387)
	at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:852)
	at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
	at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2019)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1558)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1449)
	at org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
	at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: org.postgresql.util.PSQLException: 错误: 无效的类型 integer 输入语法: "account_id_seq.nextval"
  在位置:unnamed portal parameter $9 = '...'
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2676)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2366)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:356)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:496)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:413)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:190)
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:152)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:3253)
	at com.alibaba.druid.filter.FilterAdapter.preparedStatement_executeUpdate(FilterAdapter.java:1092)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:3251)
	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.executeUpdate(PreparedStatementProxyImpl.java:194)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:255)
	at com.jfinal.plugin.activerecord.Model.save(Model.java:605)
	... 53 more


评论区

杜福忠

2022-09-20 10:10

对PostgreSQL不熟,按照普通写法,set("id", "xx") 字符串值,数据库是 int的情况都会报错吧。
“自动增长”值按说Java这边不用set的,也就是拼接的SQL INSERT 语句不需要ID才是,如果要设置规则在数据库那边的“选项”就能设置,貌似不用指定吧。
可以直接手写INSERT 语句在Navicat执行,看下没错误的SQL和JF生成的SQL是不是因为 一个是 account_id_seq.nextval 和一个是 'account_id_seq.nextval' 占位符的区别

fmpoffice

2022-09-20 13:52

@杜福忠 谢谢回复,已解决,直接remove id

l745230

2022-09-21 15:40

推荐个神器log4jdbc, 直接输出可执行的sql,放navicat里执行下就知道问题了. 不要靠猜.

热门分享

扫码入社