【问题标题】:Unable to Create Multiple Connect Query in Prisma?无法在 Prisma 中创建多个连接查询?
【发布时间】:2021-01-22 03:59:45
【问题描述】:

每当我尝试运行以下查询时,我都会收到此错误ForeignKeyConstraintViolation { constraint: Index(\"Device\") }。

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
              }
            }
          }
        })

如果我尝试 serpratly 运行一切正常,但缩短它会导致错误,为什么?

【问题讨论】:

    标签: prisma nexus-prisma prisma2 prisma-binding


    【解决方案1】:
    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
                  }
                }
              }
            })
    

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2021-07-25
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多