postgresql 正在尝试在表中创建新行,正在获取“列”accountId“不存在”,似乎正在返回新列,我不需要此列

2uluyalo  于 2022-12-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(112)

我在失眠症中工作,试图创建一个新的帐户条目。
我在terminal中得到了下面这一行,在末尾你可以看到accountId列被返回了,我不需要它。

Executing (default): INSERT INTO "accounts" ("id","name","userId","type","limit","balance","createdAt","updatedAt") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING "id","name","userId","type","limit","balance","minPayment","dueDate","createdAt","updatedAt","accountId";

因为它我得到这个错误的失眠:

column "accountId" does not exist

下面是客户模型

'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
  class Account extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      Account.hasMany(models.Transaction, { foreignKey: 'accountId' })
      Account.belongsTo(models.User, { foreignKey: 'userId' })
    }
  }
  Account.init(
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false
      },
      userId: {
        type: DataTypes.INTEGER,
        onDelete: 'CASCADE',
        references: {
          model: 'users',
          key: 'id'
        }
      },
      type: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      limit: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      balance: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      minPayment: DataTypes.INTEGER,
      dueDate: DataTypes.DATE
    },
    {
      sequelize,
      modelName: 'Account',
      tableName: 'accounts'
    }
  )
  return Account
}


以下是用户模型

'use strict'
const { Model } = require('sequelize')
module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      User.hasMany(models.Account, { foreignKey: 'accountId' })
    }
  }
  User.init(
    {
      firstName: {
        type: DataTypes.STRING,
        allowNull: false
      },
      middleName: DataTypes.STRING,
      lastName: {
        type: DataTypes.STRING,
        allowNull: false
      },
      email: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true,
        validate: {
          isEmail: true
        }
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false
      }
    },
    {
      sequelize,
      modelName: 'User',
      tableName: 'users'
    }
  )
  return User
}

我已尝试更新我的帐户模型,尝试删除外键accountId

xmq68pz9

xmq68pz91#

您在用户关联中出错:你写道:

User.hasMany(models.Account, { foreignKey: 'accountId' })

正确答案:

User.hasMany(models.Account, { foreignKey: 'userId' })

相关问题