我在失眠症中工作,试图创建一个新的帐户条目。
我在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
1条答案
按热度按时间xmq68pz91#
您在用户关联中出错:你写道:
正确答案: