Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GraphQL变异不允许我将数据添加到mysql表中,并出现非空冲突错误_Mysql_Graphql_Sequelize.js - Fatal编程技术网

GraphQL变异不允许我将数据添加到mysql表中,并出现非空冲突错误

GraphQL变异不允许我将数据添加到mysql表中,并出现非空冲突错误,mysql,graphql,sequelize.js,Mysql,Graphql,Sequelize.js,我正在使用GraphQL、Sequelize和MySql向Clients表添加数据。在GraphQl突变中,我执行以下操作: const db = require("./models"); const Mutation = new GraphQLObjectType({ name: "Mutation", fields: { addClient: { type: ClientType, args: {

我正在使用GraphQL、Sequelize和MySql向Clients表添加数据。在GraphQl突变中,我执行以下操作:

const db = require("./models");

const Mutation = new GraphQLObjectType({
    name: "Mutation",
    fields: {
        addClient: {
            type: ClientType,
            args: {
                lastName: { type: new GraphQLNonNull(GraphQLString) },
                firstName: { type: new GraphQLNonNull(GraphQLString) },
                primaryPhoneNumber: { type: new GraphQLNonNull(GraphQLString) },
                cellphone: { type: GraphQLString },
                workPhone: { type: GraphQLString },
                email: { type: GraphQLString },
                UserId: { type: new GraphQLNonNull(GraphQLString) }
            },
            resolve(parentValue, args) {
                let newClient = new db.Client({
                    lastName: args.lastName,
                    firstName: args.firstName,
                    primaryPhoneNumber: args.primaryPhoneNumber,
                    cellphone: args.cellphone,
                    workPhone: args.workPhone,
                    email: args.email,
                    UserId: args.UserId
                });
                console.log(newClient);
                return db.Client.create(newClient);
            }
        }
    }
});
但在GraphiQL上测试时,我收到了这个错误:

{
  "errors": [
    {
      "message": "notNull Violation: Client.lastName cannot be null,\nnotNull Violation: 
 Client.firstName cannot be null",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addClient"
      ]
    }
  ],
  "data": {
    "addClient": null
  }
}
我相信这个错误来自sequelize,因为在我的模型中,我定义了一些非空字段:

module.exports = function(sequelize, DataTypes) {
    var Client = sequelize.define(
        "Client",
        {
            lastName: {
                type: DataTypes.TEXT,
                allowNull: false,
                len: [1]
            },
            firstName: {
                type: DataTypes.TEXT,
                allowNull: false,
                len: [1]
            },
            primaryPhoneNumber: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            },
            cellphone: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            },
            workPhone: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            },
            email: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            }
        },

        {
            timestamps: false
        }
    );

    Client.associate = function(models) {
        // Associating Clients with Pets
        // When a Client is deleted, also delete any associated Pets
        Client.belongsTo(models.User);
        Client.hasMany(models.Pet, {
            onDelete: "cascade"
        });
    };

    return Client;
};
这是我在前端的查询:

mutation {
  addClient(lastName: "ali", firstName: "muhamed",  primaryPhoneNumber: 
"00990099009", email: "jalimaña@email.com", UserId: "14fb9610-4766-11ea-9a4e- 
e130bc08c2aa") {
    lastName,
    firstName
  }
}

有人知道为什么会这样吗?任何帮助都将不胜感激。

Model.create
将获取一个普通对象,该对象具有模型实例应初始化的值。您不应该将模型的现有实例传递给它。相反,只需对实例调用
save
方法即可

const instance = new SomeModel({ firstName: 'Bob' })
await instance.save()
这相当于

const instance = SomeModel.build({ firstName: 'Bob' })
await instance.save()


再次感谢丹尼尔·里尔登在这里帮助我。兄弟!
await SomeModel.create({ firstName: 'Bob' })