NodeJS sequileze未将UUIDV4作为默认值

ih99xse1  于 2023-02-21  发布在  Node.js
关注(0)|答案(2)|浏览(95)
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.
    • 解释**

当我写入默认值时:sequelize.UUIDV4它仍然不把它作为默认值,当我尝试把文字,它是显示abvo错误,我想要一个新的唯一的id为我的每个产品

jv2fixgn

jv2fixgn1#

请确保字段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;
  }
chhqkbe1

chhqkbe12#

您应该使用序列化. 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;
  }

相关问题