【问题标题】:sequileze is not taking UUIDV4 as default valuesequelize 没有将 UUIDV4 作为默认值
【发布时间】:2023-02-21 21:12:36
【问题描述】:
module.exports = (sequelize, Sequelize) => {
    const Product = sequelize.define("product", {
      user_id: {
        primaryKey : true,
        allowNull: false,
        type: Sequelize.UUID,
        defaultValue: Sequelize.literal(`uuid_generate_v4()`)
      },
      description: {
        type: Sequelize.STRING
      },
      rating: {
        type: Sequelize.STRING
      },
      user: {
        type: Sequelize.STRING
      },
      createdAt: {
        type: Sequelize.DATE(3),
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3)'),                
      },
      updatedAt: {
        type: Sequelize.DATE(3),
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)'),             
      }
    });
  
    return Product;
  }
(node:17672) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'uuid_generate_v4() , `description` VARCHAR(255), `rating` VARCHAR(255), `user` V' at line 1
    at Query.formatError (C:\Users\I Am Moooooti\OneDrive\ecom-sql\node_modules\sequelize\lib\dialects\mysql\query.js:265:16)
    at Query.run (C:\Users\I Am Moooooti\OneDrive\ecom-sql\node_modules\sequelize\lib\dialects\mysql\query.js:77:18)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async C:\Users\I Am Moooooti\OneDrive\ecom-sql\node_modules\sequelize\lib\sequelize.js:619:16
    at async MySQLQueryInterface.createTable (C:\Users\I Am Moooooti\OneDrive\ecom-sql\node_modules\sequelize\lib\dialects\abstract\query-interface.js:225:12)
    at async Function.sync (C:\Users\I Am Moooooti\OneDrive\ecom-sql\node_modules\sequelize\lib\model.js:1299:5)
    at async Sequelize.sync (C:\Users\I Am Moooooti\OneDrive\ecom-sql\node_modules\sequelize\lib\sequelize.js:793:35)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17672) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17672) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

说明

当我写 defaultValue : sequelize.UUIDV4 它仍然没有将它作为默认值,当我尝试输入文字时它显示 abvo 错误我想要我的每个产品的新唯一 ID

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    请确保字段user_id 在您的数据库中是否具有类型uuid

    module.exports = (sequelize, Sequelize) => {
        const Product = sequelize.define("product", {
          user_id: {
            primaryKey : true,
            allowNull: false,
            type: Sequelize.UUID,
            defaultValue: Sequelize.UUIDV4
          },
          ...
        });
      
        return Product;
      }
    

    【讨论】:

    • 它在类型列中有 char(36)
    • @Fatimasami,是的,这就是它不起作用的原因。不确定通常如何创建表,但可以更新表结构。
    【解决方案2】:

    你应该使用Sequelize.fn('gen_random_uuid')

    例子:

    module.exports = (sequelize, Sequelize) => {
        const Product = sequelize.define("product", {
          user_id: {
            primaryKey : true,
            allowNull: false,
            type: Sequelize.UUID,
            defaultValue: Sequelize.fn('gen_random_uuid')
          },
          ...
        });
      
        return Product;
      }
    

    【讨论】:

      猜你喜欢
      • 2019-08-17
      • 2018-03-05
      • 1970-01-01
      • 2021-01-10
      • 2019-10-08
      • 1970-01-01
      • 2011-01-10
      • 2018-04-11
      • 1970-01-01
      相关资源
      最近更新 更多