Hey, how can I create a new Entity and attach exis...
# orm-help
m
Hey, how can I create a new Entity and attach existing fields to it from relation? For example, I want to create a Chat and set the user field to an existing user. I tried using connect through user's id but it didn't work.
r
@Mateusz Żmijewski 👋
connect
is the way to go. What’s the query you’re making that is not working?
m
return await this.prisma.chat.create({
      
data: {
        
user: {
          
connect: {
            
id: firstUser.id,
          
},
        
},
        
secondUser: {
          
connect: {
            
id: secondUser.id,
          
},
        
},
      
},
    
});
I get this error:
Type '{ user: { connect: { id: number; }; }; secondUser: { connect: { id: number; }; }; }' is not assignable to type '(Without<ChatCreateInput, ChatUncheckedCreateInput> & ChatUncheckedCreateInput) | (Without<...> & ChatCreateInput)'.
Type '{ user: { connect: { id: number; }; }; secondUser: { connect: { id: number; }; }; }' is not assignable to type 'Without<ChatUncheckedCreateInput, ChatCreateInput> & ChatCreateInput'.
Type '{ user: { connect: { id: number; }; }; secondUser: { connect: { id: number; }; }; }' is missing the following properties from type 'ChatCreateInput': chatLineId, createdAtts(2322)
r
Could you share your schema for
Chat
and
User
?
You’re missing some field when creating a
chat
. Which is why the error occurs.
m
model Chat {
  
id                  String    @id @default(uuid())
  
user                User      @relation(fields: [userId], references: [id])
  
userId              Int
  
secondUser          User      @relation(name: "secondUser", fields: [secondUserId], references: [id])
  
secondUserId        Int
  
status              String?
  
lastMsg             ChatLine?
  
chatLineId          String
  
deletedByUser       Boolean?
  
deletedBySecondUser Boolean?
  
updatedAt           BigInt?
  
createdAt           BigInt
  
@@map("chat")
}
model User {
  
id                       Int                     @id @default(autoincrement())
  
email                    String
  
firstName                String?
  
lastName                 String?
  
externalProviderId       String?
  
externalProviderType     ExternalProviderType?
  
avatar                   String?
  
thumbUrl                 String?
  
howToAddress             HOW_TO_ADDRESS          @default(MALE)
  
about                    String?
  
topics                   String?
  
role                     UserRole
  
lat                      Decimal?
  
lng                      Decimal?
  
address                  String?
  
deliveryAddress          String?
  
deliveryLat              Decimal?
  
deliveryLng              Decimal?
  
openingHour              String?
  
closingHour              String?
  
questions                String?
  
cookOnboardingDone       Boolean                 @default(false)
  
customerOnboardingDone   Boolean                 @default(false)
  
generalPreferencesFilled Boolean                 @default(false)
  
foodPreferencesFilled    Boolean                 @default(false)
  
fcmToken                 String?
  
phoneNumber              String?
  
isPhoneNumberVerified    Boolean                 @default(false)
  
password                 String?
  
acronym                  String?
  
testMode                 Int
  
generalPreferences       UserGeneralPreferences?
  
generalPreferencesId     String
  
foodPreferences          UserFoodPreferences?
  
foodPreferencesId        String
  
orders                   Order[]
  
meals                    Meal[]
  
mealId                   String
  
chatsAsFirst             Chat[]
  
chatAsFirstId            String
  
chatsAsSecond            Chat[]                  @relation(name: "secondUser")
  
chatsAsSecondId          String
  
messages                 ChatLine[]
  
chatLineId               String
  
images                   Pictures                @relation(fields: [imagesId], references: [id])
  
imagesId                 String
  
reports                  Report[]
  
issuedRecommendations    Recommendation[]        @relation(name: "issuer")
  
recommendationAsIssuerId String
  
recommendations          Recommendation[]
  
recommendationId         String
  
updatedAt                BigInt
  
createdAt                BigInt
  
favouriteCooks           User                    @relation(name: "FavouriteCooks", fields: [cookId], references: [id])
  
favouritedBy             User?                   @relation(name: "FavouriteCooks")
  
cookId                   Int
  
@@map("user")
}
r
You’re missing passing
chatLineId
and
createdAt
from the
Chat
model. Those are required.
m
Gotcha, thank you!
👍 1