mysql - 我如何使用 sequelize ORM 将列定义为 tinyint(4)

标签 mysql sql node.js sequelize.js

我需要将我的表定义为

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `o_id` int(11) unsigned NOT NULL,
  `m_name` varchar(45) NOT NULL,
  `o_name` varchar(45) NOT NULL,
  `customer_id` int(11) unsigned NOT NULL,
  `client_id` tinyint(4) unsigned DEFAULT '1',
  `set_id` tinyint(4) unsigned DEFAULT NULL,
  `s1` tinyint(4) unsigned DEFAULT NULL,
  `s2` tinyint(4) unsigned DEFAULT NULL,
  `s3` tinyint(4) unsigned DEFAULT NULL,
  `review` varchar(2045) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `br_o_id_idx` (`order_id`),
  KEY `br_date_idx` (`created_at`),
  KEY `br_on_idx` (`operator_name`),
  KEY `br_mn_idx` (`merchant_name`)
)

但是当我查看 sequelize 文档时,它不支持 tiny int 及其大小。

最佳答案

由于您的表定义中缺少 ZERO_FILL,我怀疑 tinyint(4) 可能没有按照您的预期进行。来自 11.2.5 Numeric Type Attributes :

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type.

...

The display width does not constrain the range of values that can be stored in the column.

...

For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

我不确定其他 RDBMS 是否以不同方式处理括号中的数字,但仔细阅读 sequelize source看起来他们对你的印象是一样的,但都是错误的。

也就是说,您的模式的重要部分是您希望将这些字段存储为 TINYINT(仅使用一个字节的存储空间来包含 0-255 之间的值),令人遗憾的是在 Sequelize 数据类型中不可用。我可能会建议打开一个 PR 来添加它......

另一方面,如果您真的在寻找 ZEROFILL 功能,并且需要指定 显示宽度 为 4,您可以执行类似 Sequelize.INTEGER(4) 的操作。 ZEROFILL,但显然,这会非常浪费您的数据库中的空间。

关于mysql - 我如何使用 sequelize ORM 将列定义为 tinyint(4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38895259/

相关文章:

java - SQL查询返回数据但ResultSet为空

c# - LINQ to Entities - 更新二进制字段

mysql - 如何在 CodeIgniter 中将 unicode 表情符号存储到 MYSQL

sql - 什么是 "Advanced"SQL?

mysql - 使用 n :m-relation 复制数据集

mysql - 不能让你的两个或多个方法在同一条路线上

javascript - NodeJS v15.7.0 | Electron v11.2.3 | Uncaught Error : Uncaught TypeError: Class constructor <Class> cannot be invoked without 'new' , 但是什么?

javascript - Guard 不在 nestJs 中执行 passport 策略

mysql - 数据库中项目动态类别的良好解决方案

php - 图表中来自数据库的 fusionCharts 数据