Data truncation: Out of range value for column ‘allow_invite‘ at row 1

devtools/2025/3/6 21:49:54/

在这里插入图片描述

  • 由于前端传递的数值超过了mysql数据库中tinyint类型的取值范围,所以就会报错。
Caused by: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Out of range value for column 'allow_invite' at row 1at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:104)at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:974)at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1113)at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061)at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1381)at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1046)at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2843)at com.alibaba.druid.filter.FilterAdapter.preparedStatement_executeUpdate(FilterAdapter.java:1091)at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_executeUpdate(FilterEventAdapter.java:491)at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_executeUpdate(FilterChainImpl.java:2841)at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.executeUpdate(PreparedStatementProxyImpl.java:194)at com.alibaba.druid.pool.DruidPooledPreparedStatement.executeUpdate(DruidPooledPreparedStatement.java:256)at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)... 146 more
-- auto-generated definition
create table invite_codes
(id                 int auto_incrementprimary key,admin_id           int                                  null,bound_phone        varchar(20)                          null,bound_wx_uid       varchar(255)                         null,created_date       timestamp  default CURRENT_TIMESTAMP not null,expire_time        datetime                             null,generated_date     datetime                             not null,invite_code        varchar(255)                         not null,invite_level       int                                  null,is_locked          tinyint(1) default 0                 null,last_modified_date timestamp                            null on update CURRENT_TIMESTAMP,remark             text                                 null,status             tinyint    default 0                 not null,user_id            int                                  null,weixin_headimg     varchar(255)                         null,weixin_nickname    varchar(255)                         null,invitor            int                                  not null,allow_invite       tinyint    default 0                 not null
)engine = MyISAM;create index FKnqn27fyjlgio5y60eieohi0bfon invite_codes (invitor);

根据你提供的数据库创建脚本,allowInvite 字段在数据库中的定义如下:

allow_invite       tinyint    default 0                 not null

分析

  • 数据类型tinyint
  • 默认值0
  • 是否为空not null

关键点

  1. 有符号 vs 无符号

    • 默认情况下tinyint 在 MySQL 中是有符号的,取值范围是 -128127
    • 无符号:如果字段定义中包含 unsigned 关键字,则取值范围是 0255
  2. 你的字段定义

    • 在你的创建脚本中,allow_invite 字段没有 unsigned 关键字,因此它是有符号的 tinyint

结论

  • 取值范围allow_invite 字段的取值范围是 -128127
  • 前端传递 999 的结果:由于 999 超出了有符号 tinyint 的最大值 127,前端传递 999 会导致数据库插入或更新时报错。

解决方案

为了支持 999 或更大的值,你可以将 allow_invite 字段改为无符号的 tinyint 或者更大的数据类型。以下是两种解决方案:

tinyint_84">1. 将 allow_invite 改为无符号的 tinyint
ALTER TABLE invite_codes MODIFY COLUMN allow_invite tinyint unsigned default 0 not null;
2. 将 allow_invite 改为 smallint
ALTER TABLE invite_codes MODIFY COLUMN allow_invite smallint default 0 not null;
  • smallint 的取值范围是 -3276832767(有符号)或 065535(无符号),可以更好地支持更大的值。

tinyint_100">示例:修改为无符号的 tinyint

ALTER TABLE invite_codes MODIFY COLUMN allow_invite tinyint unsigned default 0 not null;

示例:修改为 smallint

ALTER TABLE invite_codes MODIFY COLUMN allow_invite smallint default 0 not null;

更新实体类

确保在 Java 实体类中更新相应的字段定义,以匹配数据库的变化。例如,如果改为无符号的 tinyint

@Column(columnDefinition = "tinyint unsigned default 0")
@ApiModelProperty(value = "允许邀请的层级,默认为0(不允许)," +"1表示该用户可以生成邀请码,但其生成的邀请码对应的用户不能继续生成邀请码;" +"2表示该用户可以生成邀请码,其生成的邀请码对应的用户也可以生成邀请码,但再下一级用户不能生成邀请码;" +"999表示用户可以生成邀请码,并且可以无限传递下去;", required = true, example = "1")
private Integer allowInvite;

或者如果改为 smallint

@Column(columnDefinition = "smallint default 0")
@ApiModelProperty(value = "允许邀请的层级,默认为0(不允许)," +"1表示该用户可以生成邀请码,但其生成的邀请码对应的用户不能继续生成邀请码;" +"2表示该用户可以生成邀请码,其生成的邀请码对应的用户也可以生成邀请码,但再下一级用户不能生成邀请码;" +"999表示用户可以生成邀请码,并且可以无限传递下去;", required = true, example = "1")
private Integer allowInvite;

通过这些修改,你可以确保 allowInvite 字段能够支持 999 或更大的值,从而避免前端传递 999 时出现数据库错误。

TinyInt 数据类型 有符号: -128 至 127 无符号: 0 至 255 使用 1 字节存储

http://www.ppmy.cn/devtools/165076.html

相关文章

Web后端开发-总结

一.技术总结 web后端开发现在基本上都是基于标准的三层架构进行开发的:Controller层,也叫控制器层,负责接受请求响应数据。service层,也叫业务器层,负责业务逻辑的实现。dao层,也叫数据访问层,持…

【长安大学】苹果手机/平板自动连接认证CHD-WIFI脚本(快捷指令)

背景: 前几天实在忍受不了CHD-WIFI动不动就断开,一天要重新连接,点登陆好几次。试了下在网上搜有没有CHD-WIFI的自动连接WIFI自动认证脚本,那样我就可以解放双手,随时用WIFI就行了,但是没有找到。于是我就…

DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)示例3: 行选择

前言:哈喽,大家好,今天给大家分享一篇文章!并提供具体代码帮助大家深入理解,彻底掌握!创作不易,如果能帮助到大家或者给大家一些灵感和启发,欢迎收藏关注哦 💕 目录 Deep…

SQL Server数据库中用存储过程来取顺序号

SQL Server数据库中用存储过程来取顺序号 表sys_number,字段name字符,表示前缀,value数字,一个一个递增 存储过程代码: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[usp_GetSysid]name varchar(5…

C/C++输入输出(1)

1.getchar和putchar 1.1getchar() 函数原型: 1 int getchar(void); getchar()函数返回用户从键盘输入的字符,使用时不带有任何参数。 程序运行到这个命令就会暂停,等待用户从键盘输入,等同于使用cin或scanf()方法读取一个字符…

PHP 包含(Include)机制详解

PHP 包含(Include)机制详解 在PHP编程中,include和require是两个非常基础的函数,用于在脚本中包含其他文件。它们在模块化编程中发挥着至关重要的作用,使得代码更易于维护和扩展。本文将详细介绍PHP的包含机制,包括其工作原理、使用方法以及最佳实践。 一、PHP 包含机制…

JAVA实战开源项目:安康旅游网站(Vue+SpringBoot) 附源码

本文项目编号 T 098 ,文末自助获取源码 \color{red}{T098,文末自助获取源码} T098,文末自助获取源码 目录 一、系统介绍二、数据库设计三、配套教程3.1 启动教程3.2 讲解视频3.3 二次开发教程 四、功能截图五、文案资料5.1 选题背景5.2 国内…

Python项目-基于深度学习的校园人脸识别考勤系统

引言 随着人工智能技术的快速发展,深度学习在计算机视觉领域的应用日益广泛。人脸识别作为其中的一个重要分支,已经在安防、金融、教育等多个领域展现出巨大的应用价值。本文将详细介绍如何使用Python和深度学习技术构建一个校园人脸识别考勤系统&#…