question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow field default to reference other fields

See original GitHub issue

Problem

I have a model where there is the concept of a unique name and a displayName. Think GitHub organizations. I would like to encode in the model that the default for the displayName field is name field.

image

Suggested solution

An expression that references another non-nullable field whose type is the same.

model Workspace {
  kind        WorkspaceKind @default(workspace)
  id          String        @id @default(uuid())
  name        String        @unique
  displayName String        @default(field(name))        // <-- example
  members     User[]
}

Alternatives

It is easy to encode this at the application layer.

Additional context

This is a NTH (nice to have). The theme is encoding as much as possible at the database layer so that the application layer is easier to reason about.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
do4grcommented, Feb 2, 2021

I had a quick look whether the new arbitrary sql in @default(dbgenerated()) would allow that. It seems that Postgres and Mysql do not directly support that in the default definition. So we’d indeed need a completely new feature for this.

0reactions
leafysharkcommented, Sep 9, 2022

Hi guys, is this something you decided to progress? I have two fields:

endTime: DateTime (which would be some day in the future) 24HoursAheadOfEndTime: DateTime @default(dbgenerated("NOW() + INTERVAL '1 DAY'"))

And I would like to replace NOW() with endTime field, is this possible at all or not?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reference Object fields when setting default values
When setting a default value for any field on a specific object, I cannot reference that object within the formula. However this can...
Read more >
Set default values for fields or controls - Microsoft Support
Set a default value​​ Select the field that you want to change. On the General tab, type a value in the Default Value...
Read more >
How to pass default value to custom field to reference other field
I have an custom object name PowerPlantIntroduce__c and have two fields: The first one is pictureUrl__c (Data Type: URL. Used to save picture's ......
Read more >
How to set default value of a reference field base... - ServiceNow
I have a reference variable for location of the user and another reference variable within the form to display the 'requested for' user....
Read more >
Django Model Field Default Based Off Another Field in Same ...
Is this possible in Django, having a callable function give a default to some field based on the value of another field?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found