【问题标题】:Error on INSERT INTO command with Sequelize?Sequelize 的 INSERT INTO 命令出错?
【发布时间】:2022-02-15 00:13:13
【问题描述】:

我在尝试模拟我的数据库时遇到了一些奇怪的错误。

首先,这是我的用户模型(使用默认时间戳,因为我没有在选项中添加任何内容):

const { Sequelize, Model, DataTypes } = require("sequelize");
const sequelize = require("../db");

const User = sequelize.define("user", {
  id: {
    type: DataTypes.INTEGER, // or maybe Type STRING?
    autoIncrement: true,
    primaryKey: true,
    allowNull: false,
  },
  first_name: {
    type: DataTypes.STRING,
  },
  last_name: {
    type: DataTypes.STRING,
  },
  email: {
    type: DataTypes.STRING,
    unique: true,
    allowNull: false
  },
  password: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  number_of_answers: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
  }
});


module.exports = User;

我已经有一个通过注册端点插入的用户: Screenshot from psql - SELECT * FROM users;

现在,我正在尝试插入用户,并使用 Mockaroo 创建了一个 sql 文件。这是一行的示例,以及我尝试运行此命令时遇到的错误。

psql error on INSERT INTO command

有谁知道这里有什么问题以及如何通过 psql 在集合中插入用户。

如果您想知道我的注册端点是如何工作的,这是我的代码:

// User Signup
router.post("/signup", async (req, res, next) => {
  try {
    // Crypting password
    const hashedPw = await bcrypt.hash(req.body.password, 12);
    const results = await User.create({
      first_name: req.body.firstName,
      last_name: req.body.lastName,
      email: req.body.email,
      password: hashedPw,
    });
    res.status(201).json({
      status: "User saved to database",
      data: {
        results: results,
      },
    });
  } catch (err) {
    next(err);
  }
});

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    好的,我发现我需要把属性放在“”之间..所以它会像下面这样..

    INSERT INTO users ("id", ...,"createdAt" ..) VALUES ("idValue", ...);
    

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多