Unable to Create Multiple Connect Query in Prisma?

543 views Asked by At

I am getting this error ForeignKeyConstraintViolation { constraint: Index(\"Device\") } whenever i try to run following query.

const _CreatedConnectedToMobile = await Context.Database.ConnectedToMobile.create({ 'data': {} }) <-- creates entry in db with default values.

// Create mobile entry where i am trying to create entry in Account Table, with Connection to Device and ConnectedToMobile Table.
// Account, Device and ConnectedToMobile -> Mobile(Table) by foriegn key.
await Context.DataBase.mobile.create({
          'data': {
            'number': mobileNumber,
            'Account': {
              'create': {
                'accountType': _accountType,
                'activeAs': _accountType,
                'accessPin': accessPin
              }
            },
            'Device': {
              'connectOrCreate': {
                'where': { deviceUniqueId },
                'create': { deviceUniqueId }
              }
            },
            'ConnectedToMobile': {
              'connect': {
                'id': _CreatedConnectedToMobile.id
              }
            }
          }
        })

if i try to run serpratly everythings work fine but shortning it causes error why ?

1

There are 1 answers

0
Ahmed Elywa On
await Context.DataBase.mobile.create({
          'data': {
            'number': mobileNumber,
            'Account': {
              'create': {
                'accountType': _accountType,
                'activeAs': _accountType,
                'accessPin': accessPin
              }
            },
            'Device': {
              'connectOrCreate': {
                'where': { deviceUniqueId }, // you search by unique Id
    // when you create you send object of data
                'create': { deviceUniqueId } // the issue here you send the unique Id but this object accept all fields in your schema model except the ID because Id will auto generate by id
              }
            },
            'ConnectedToMobile': {
              'connect': {
                'id': _CreatedConnectedToMobile.id
              }
            }
          }
        })