gpt4 book ai didi

node.js - Postgres 尝试返回我的模型中不存在的列

转载 作者:行者123 更新时间:2023-12-03 22:18:20 33 4
gpt4 key购买 nike

我使用 graphql API 并尝试将数据插入 Postgress 表并出现错误:

"message": "column \"UserId\" does not exist",
和我的原始查询:
Executing (default): INSERT INTO "Recipes" 
("id","title","ingredients","direction","createdAt","updatedAt","userId") VALUES
(DEFAULT,$1,$2,$3,$4,$5,$6)
RETURNING
"id","title","ingredients","direction","createdAt","updatedAt","userId","UserId";
问题是列 UserId 不在我的模型中,但 userId 是!而且我不知道为什么 Postgres 试图返回 UserId 列。
我的模型。
用户:
module.exports = (sequelize, DataTypes) => {
class User extends Model {
static associate(models) {
User.hasMany(models.Recipe)
}
};
User.init({
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'User',
});
return User;
};
食谱:
module.exports = (sequelize, DataTypes) => {
class Recipe extends Model {
static associate(models) {
Recipe.belongsTo(models.User, { foreignKey: 'userId' })
}
};
Recipe.init({
title: {
type: DataTypes.STRING,
allowNull: false
},
ingredients: {
type: DataTypes.STRING,
allowNull: false
},
direction: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'Recipe',
});
return Recipe;
};
我的 graphql 架构:
const typeDefs = gql`
type User {
id: Int!
name: String!
email: String!
recipes: [Recipe!]!
}

type Recipe {
id: Int!
title: String!
ingredients: String!
direction: String!
user: User!
}

type Query {
user(id: Int!): User
allRecipes: [Recipe!]!
recipe(id: Int!): Recipe
}

type Mutation {
createUser(name: String!, email: String!, password: String!): User!
createRecipe(
userId: Int!
title: String!
ingredients: String!
direction: String!
): Recipe!
}
`
graphql reslover:
  Mutation: {
async createRecipe (root, { userId, title, ingredients, direction }, { models }) {
return models.Recipe.create({ userId, title, ingredients, direction })
}
}
和我的 grapql 请求:
mutation {
createRecipe(
userId: 1
title: "Sample 2"
ingredients: "Salt, Pepper"
direction: "Add salt, Add pepper"
) {
id
title
ingredients
direction
}
}

最佳答案

我在做一个项目时遇到了类似的问题;指定 parent 和 child 的关系解决了我的问题:
用户:

module.exports = (sequelize, DataTypes) => {

class User extends Model {
static associate(models) {
User.hasMany(models.Recipe, {as: 'recipes', foreignKey:'userId'})
}
};
User.init({
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'User',
});
return User;
};
食谱:
module.exports = (sequelize, DataTypes) => {
class Recipe extends Model {
static associate(models) {
Recipe.belongsTo(models.User, { foreignKey: 'userId' })
}
};
Recipe.init({
title: {
type: DataTypes.STRING,
allowNull: false
},
ingredients: {
type: DataTypes.STRING,
allowNull: false
},
direction: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'Recipe',
});
return Recipe;
};

关于node.js - Postgres 尝试返回我的模型中不存在的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64441761/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com