``` model Account { Id BigInt @unique @...
# orm-help
u
Copy code
model Account {
  Id       BigInt    @unique @default(autoincrement()) @db.BigInt
  Email    String    @unique
  Password String
  Roles    Role[]
  RoleIds  BigInt[]  @unique @db.BigInt
}

model Role {
  Id        BigInt   @unique @default(autoincrement()) @db.BigInt
  Name      String
  Enum      String
  Account   Account? @relation(fields: [Id], references: [RoleIds])
}

anyone know how to implement RoleIds reference correctly?
r
@Urmo R 👋 Could you explain what you’re trying to achieve here?
If you just want to store a 1-many relationship, only this is required:
Copy code
model Account {
  Id       BigInt @unique @default(autoincrement()) @db.BigInt
  Email    String @unique
  Password String
  Roles    Role[]
}

model Role {
  Id        BigInt   @unique @default(autoincrement()) @db.BigInt
  Name      String
  Enum      String
  Account   Account? @relation(fields: [accountId], references: [Id])
  accountId BigInt?
}
This makes sure that 1 Account can have many roles. You can read more about how to model relations here.
u
Hey, thanks , but that dosent solve the issue
i will explain
const Roles = [     {         Enum: 'ROLE_ADMIN'     },     {         Enum: 'ROLE_EDIT_PROJECTS'     },     {         Enum: 'ROLE_ADD_PROJECTS'     },     {         Enum: 'ROLE_REMOVE_PROJECTS'     } ]; If i want to add specific roles to Account then it should look like this: const Account = [     {         Name: 'Someone',         Email: 'someone@example.com',         Roles: [             {                 Enum: 'ROLE_EDIT_PROJECTS'             },             {                 Enum: 'ROLE_ADD_PROJECTS'             },             // and he cant remove projects         ]     } ] its bcs i dont want duplicate data with multiple roles data with attached accountid to role
r
This can be easily achieved with the above model, you can add multiple roles to the account and reference them via a relation. You do not need a separate
RoleIds
field as the mapping will be done automatically.
u
your right... thanks i get it now
💯 1
But i still need this implementation in somewhere else
Not with roles
for eg. if i need to share task with certain accountIds
r
Same thing, create a relation with the
Task
model and
Account
model. For e.g.
Account
can have many
Task
so it will be a 1-many relation and you can share them with Accounts.
u
cant get it
model Account {   Id          BigInt    @unique @default(autoincrement()) @db.BigInt   Email       String    @unique   Password    String   Roles       Role[]   Tasks       Task[]   SharedTasks Task[] @relation("SharedProjects") } model Task {   Id               BigInt  @unique @default(autoincrement()) @db.BigInt   Name             String   Description      String?   OwnerAccount     Account @relation(fields: [OwnerAccountId], references: [Id])   OwnerAccountId   BigInt  @db.BigInt   SharedAccounts   Account @relation(name: "SharedProjects", fields: [SharedAccountIds], references: [Id])   SharedAccountIds BigInt[] }
SharedAccountIds BigInt[] - need to use this here
or im wrong?
r
So many accounts can have the same task associated with them and also many Tasks can be related to an Account. Am I correct?
u
YES
sry caps
ahh i can do extra model
thanks
r
Your final schema will look like this:
Copy code
model Account {
  Id       BigInt @id @default(autoincrement()) @db.BigInt
  Email    String @unique
  Password String
  Roles    Role[]
  Tasks    Task[]
}

model Role {
  Id        BigInt   @id @default(autoincrement()) @db.BigInt
  Name      String
  Enum      String
  Account   Account? @relation(fields: [accountId], references: [Id])
  accountId BigInt?
}

model Task {
  Id          BigInt    @id @default(autoincrement()) @db.BigInt
  Name        String
  Description String?
  Accounts    Account[]
}
Also adding
@id
is better as that will be your primary key and will be unique by default.
u
Looks more like so
👍 1