Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateItems does not give defaultValue #2951

Closed
EricKit opened this issue May 12, 2020 · 2 comments
Closed

CreateItems does not give defaultValue #2951

EricKit opened this issue May 12, 2020 · 2 comments

Comments

@EricKit
Copy link

EricKit commented May 12, 2020

Bug report

I’m guessing not all the mutation lifecycles are executed on createItems.

Describe the bug

When using createItems on a field with a default values (with our without required) the default value is not applied.

With Knex it gives: I get Error: null value in column “comment” violates not-null constraint. With Mongoose, it creates the item without a default value.

To Reproduce

Steps to reproduce the behaviour. Please provide code snippets or a repository:

npm init keystone-app my-app

Select the Start with Auth
Add bugField to fields of user:

keystone.createList("User", {
  fields: {
    name: { type: Text },
    email: {
      type: Text,
      isUnique: true,
    },
    isAdmin: {
      type: Checkbox,
      // Field-level access controls
      // Here, we set more restrictive field access so a non-admin cannot make themselves admin.
      access: {
        update: access.userIsAdmin,
      },
    },
    password: {
      type: Password,
    },
    bugField: {
      required: true,
      type: Text,
      defaultValue: "test",
    },
  },
  // List-level access controls
  access: {
    read: access.userIsAdminOrOwner,
    update: access.userIsAdminOrOwner,
    create: access.userIsAdmin,
    delete: access.userIsAdmin,
    auth: true,
  },
});

In initial data add:

  await keystone.createItem({
    User: {
      name: "Bill",
      email: "bill@test.com",
      isAdmin: false,
      password: "password",
    },
  });

You will see that with mongoose you will get two users. the first was the one with the executeQuery which has a default bugField. You will see Bill does not have a default bugField. Adding or removing required makes no difference.

Expected behaviour

All lifecycles are executed, including default fields.

Bill should have a bugField that contains the value test.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

MacOS
"@keystonejs/adapter-mongoose": "^8.1.0",
"@keystonejs/app-admin-ui": "^6.0.0",
"@keystonejs/app-graphql": "^5.1.7",
"@keystonejs/auth-password": "^5.1.7",
"@keystonejs/fields": "^10.0.0",
"@keystonejs/keystone": "^9.0.0",

@pahaz
Copy link
Contributor

pahaz commented May 18, 2020

to look the same as #2852

@MadeByMike
Copy link
Contributor

Yes that is correct not all the lifecycle events are connected to createItems. For that use executeQuery. createItems is not meant to be relied upon for seeding production data and is more a stop-gap for getting initial data into demo.

I'm going to create a task for myself to update all the demos and docs to help avoid this until createItems can be removed: #1091

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants