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.

Abstract Model for common fields

See original GitHub issue

Problem

I think common fields can be excluded from models and be in one single abstract model, also I find it can be a little bit redundant to have to write them over and over again.

Suggested solution

Have an abstract modifier before the model to exclude it in the database and all the models that extends it can have all the predefined fields.

Alternatives

Abstract modifler

abstract model BaseFields {
  id        String   @id @default(uuid()) @db.VarChar(36)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Implementation

model User extends BaseFields {
  username  String   @unique @db.VarChar(20)
  password  String   @unique
  Post      Post[]

  @@map("users")
}

model Post extends BaseFields {
  title     String
  published Boolean @default(false)
  author    User    @relation(fields: [authorId], references: [id])
  authorId  String

  @@map("posts")
}

Result

model User extends BaseFields{
  id        String   @id @default(uuid()) @db.VarChar(36) // auto generated filed or ingected?
  createdAt DateTime @default(now()) // auto generated filed or ingected?
  updatedAt DateTime @updatedAt // auto generated filed or ingected?
  username  String   @unique @db.VarChar(20)
  password  String   @unique
  Post      Post[]

  @@map("users")
}

model Post extends BaseFields {
  id        String   @id @default(uuid()) @db.VarChar(36) // auto generated filed or ingected?
  createdAt DateTime @default(now()) // auto generated filed or ingected?
  updatedAt DateTime @updatedAt // auto generated filed or ingected?
  title     String
  published Boolean  @default(false)
  author    User     @relation(fields: [authorId], references: [id])
  authorId  String

  @@map("posts")
}

Additional context

I’m open to all comments, so please leave something useful. This is my first post here, please be kind. If this issue is duplicated, mention it.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:28
  • Comments:5

github_iconTop GitHub Comments

3reactions
Hexa-ITcommented, Jul 15, 2022

+1 Would love to see this feature implemented!

2reactions
yanaverycommented, Jun 17, 2022

+1 for this feature. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using abstract models in Django - DEV Community ‍ ‍
All you have to do is create a base model with all the common fields and set abstract = True in the meta...
Read more >
Python Snippets: Querying Abstract Models
Abstract Base Classes. Abstract models allow for common fields to be placed in a parent class, but tables are only created for derived...
Read more >
How to create Abstract Model Class in Django? - GeeksforGeeks
So here I create a model common and put the common fields in that model. Put abstract = True in the Meta class....
Read more >
How to create an abstract model class in Django - Dev Genius
“Abstract base classes are useful when you want to put some common information into a number of other models. You write your base...
Read more >
How to create an admin mixin for common/abstract model in ...
The TenantAwareModel is an abstract model that allows you to avoid repeating the common fields in the definition of others models, ...
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