Postgress tries to return a column that does not exist in my model

Viewed 978

I use graphql API and try to insert data into Postgress table and got an error:

"message": "column \"UserId\" does not exist",

and my raw query:

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";

the problem is that a column UserId isn't in my model but userId is ! And i don't know why Postgres trying to return UserId column. My Models. User:

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;
};

Recipe:

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;
};

my graphql schema:

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 })
      }
  }

and my grapql request:

mutation {
  createRecipe(
    userId: 1
    title: "Sample 2"
    ingredients: "Salt, Pepper"
    direction: "Add salt, Add pepper"
  ) {
    id
    title
    ingredients
    direction
  }
}
1 Answers

I had a similar issue while working on a project; specifying the relationship on both the parent and the child solved my issue:

User:

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;
};

Recipe:

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;
};
Related