GraphQL的Sequelize中的OneToMany关联。自定义外键

GraphQL的Sequelize中的OneToMany关联。自定义外键,graphql,sequelize.js,Graphql,Sequelize.js,我正试图在Sequelize的帮助下为GraphQL创建一个Apollo服务器 我已经阅读了Sequelize的文档和一些教程,但仍然有错误: User.hasMany(models.Recipe) Recipe.belongsTo(models.User,{foreignKey:'userId'}) 但当我在GraphQL中执行一个变异以在我的数据库中添加一个新配方时,我观察我的控制台,它正在执行以下查询: INSERT INTO "Recipe" ("id&qu

我正试图在Sequelize的帮助下为GraphQL创建一个Apollo服务器

我已经阅读了Sequelize的文档和一些教程,但仍然有错误:

User.hasMany(models.Recipe)

Recipe.belongsTo(models.User,{foreignKey:'userId'})

但当我在GraphQL中执行一个变异以在我的数据库中添加一个新配方时,我观察我的控制台,它正在执行以下查询:

INSERT INTO "Recipe" ("id","title","ingredients","direction", "userId") VALUES (DEFAULT,$1,$2,$3) RETURNING "id","title","ingredients","direction","userId", **"UserId"**;
它在返回时比预期多了一个字段,返回了什么和错误,我不知道为什么

"message": "Doesn't exist column «UserId»",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createRecipe"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "name": "SequelizeDatabaseError",
          "parent": {
            "length": 242,
            "name": "error",
            "severity": "ERROR",
            "code": "42703",
            "hint": "You probably want to refer column «Recipe.userId».",
我知道我可以更改foreignKey并用大写字母('UserId')将其命名,这将解决问题,但我希望能够自定义自己的外键

根据要求,以下是我的迁移:

await queryInterface.createTable('Recipe', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      userId: {
        type: Sequelize.INTEGER,
        allowNull: false
      },
      title: {
        allowNull: false,
        type: Sequelize.STRING
      },
      ingredients: {
        allowNull: false,
        type: Sequelize.STRING
      },
      direction: {
        allowNull: false,
        type: Sequelize.STRING
      }
    });

await queryInterface.createTable('User', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      email: {
        allowNull: false,
        type: Sequelize.STRING
      },
      password: {
        allowNull: false,
        type: Sequelize.STRING
      }
    });
这是de models/recipe.js:

module.exports = (sequelize, DataTypes) => {
  const Recipe = sequelize.define('Recipe', {
    title: {
      type: DataTypes.STRING,
      allowNull: false
    },
    ingredients: {
      type: DataTypes.STRING,
      allowNull: false
    },
    direction: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    modelName: 'Recipe',
    freezeTableName: true,
    timestamps: false
  });
  Recipe.associate = function(models) {
    Recipe.belongsTo(models.User);
  };
  return Recipe;
};
这是schema.js:

const { gql } = require('apollo-server')

const typeDefs = gql`
    type Query {
        user(id: Int!): User
        allRecipes: [Recipe!]!
        recipe(id: Int!): Recipe
    }

    type User {
        id: Int!
        name: String!
        email: String!
        recipes: [Recipe!]!
      }

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

    type Mutation {
        createUser(name: String!, email: String!, password: String!): User!
        createRecipe(
          userId: Int!
          title: String!
          ingredients: String!
          direction: String!
        ): Recipe!
    }
`

module.exports = typeDefs

谢谢

您还应在
hasMany
中指出
foreignKey
选项:

User.hasMany(models.Recipe, { foreignKey: 'useId' })

您还应在
hasMany
中指出
foreignKey
选项:

User.hasMany(models.Recipe, { foreignKey: 'useId' })

显示模型定义。我想您在描述
userId
@Anatoly时没有指明字段名,我已经编辑了它来显示模型。这就是导致返回userId和userId的原因吗?您添加了迁移,但我希望看到用于查询的模型定义recipes@Anatoly你是这个意思吗?谢谢显示模型定义。我想您在描述
userId
@Anatoly时没有指明字段名,我已经编辑了它来显示模型。这就是导致返回userId和userId的原因吗?您添加了迁移,但我希望看到用于查询的模型定义recipes@Anatoly你是这个意思吗?谢谢