Scaffold and default values

So I created a scaffold based on the schema, but all the creationdate should had a default value as now, instead form marked it as required, is something missing?

Hi. @khatyan.

I’m going to take a guess – so let me know if I am on the right path or I have made too many assumptions.

If you could share

  • the part of your schema for the model in the form
  • its sdl
  • markup used in the form cell/code in page
  • screenshot

that would help tremendously.

But let’s say you have:

model Post {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  title     String
  body      String
}

The createdAt wil get set the default value when saved. It does not initiate an empty model with the value.

In fact, the SDL for input to create or update shouldn’t have createdAt there:

  input CreatePostInput {
    title: String!
    body: String!
  }

  input UpdatePostInput {
    title: String
    body: String
  }

That’s because it will be overwritten when saved.

Therefore, there is not much sense in setting the createdAt value in a form for create or update because you cannot change the value (just like one cannot set its id) – and even if you did initialize Post with it, you would be “too young” because the date would be before it actually was persisted.

Does this help?

If not, please include some code examples (see above) and a screenshot – and what you’d like to actually see and happy to help.

Hi @khatyan,

This is a known limitation for any fields not specifically having createdAt or updatedAt as key from the prisma.schema - the open issue is: https://github.com/redwoodjs/redwood/issues/766. The example from @dthyresson does in fact utilise these keys and hence these are excluded from the generated DSL.

You can get around this by either manually changing the DSL and the client side code. Alternatively myself and Rob made a few comments in the issue that could help you guide to fixing this issue for all fields that have specified a default such as admin @default(false)

For your reference her is the exact line of code: https://github.com/redwoodjs/redwood/blob/main/packages/cli/src/commands/generate/sdl/sdl.js#L23

1 Like

@Rosenberg96 Thanks for pointing me to this issue. Makes much more sense now. :slight_smile:

I thank you from bottom of my heart.

1 Like