简体   繁体   English

如何从Prisma中的MySql关系表获取数据

[英]How to get data from MySql relation table in Prisma

In datamodel.graphql 在datamodel.graphql中

type Ride {
 rideId: String
 productId: String
 passenger: Passenger
 origin: Origin
 destination: Destination
 dateTime: DateTime
 feedback: String
}


type Passenger {
 id: ID! @unique
 firstName: String
 lastName: String
}

type Destination {
 # The unique ID of the destination.
 id: ID! @unique
 latitude: Float
 longitude: Float
 address: String
}

type Origin {
 id: ID! @unique
 latitude: Float
 longitude: Float
 address: String
}


type Report {

 productId: String
 passenger: Passenger
 description: String
}

I deployed this data model and generates a MySql Db, auto queries, mutations with this. 我部署了此数据模型,并生成了一个MySql Db,自动查询和与此相关的变异。

It creates a "Ride", "Passenger", "Report" and "Origin" table in MySql. 它在MySql中创建一个“ Ride”,“ Passenger”,“ Report”和“ Origin”表。 But it didn't create any column for passenger, origin, destination in "Ride" table. 但是它没有在“乘车”表中为旅客,始发地和目的地创建任何列。

It separates creates a relation table for this like _PassengerToRide, _OriginToRide, and _DestinationToRide. 它为此单独创建一个关系表,例如_PassengerToRide,_OriginToRide和_DestinationToRide。

Lack of relation establishment in "Ride" table, I couldn't get the details from Passenger, Origin and Destination tables when I query "rides()". 在“乘车”表中没有建立关系,当我查询“ rides()”时,无法从“乘客”,“始发地”和“目的地”表中获取详细信息。 Is this the correct way to define the datamodel.graphql. 这是定义datamodel.graphql的正确方法吗? (edited) (编辑)

Based on your description, this query should "just work": 根据您的描述,此查询应“有效”:

query {
  rides {
    feedback
    passenger {
      id
    }
    origin {
      id
    }
    destination {
      id
    }
  }
}

Prisma uses the relation table approach you mentioned to keep track if relations between two nodes, for example table _OriginToRide relates to relation @relation(name: "OriginToRide") from your datamodel. Prisma使用您提到的关系表方法来跟踪两个节点之间的关系,例如表_OriginToRide与数据模型中的@relation(name: "OriginToRide")关系相关。

You don't have to change anything on the SQL level to connect the relations afterwards. 之后,您无需在SQL级别上进行任何更改即可连接关系。

Note: The above applies to Prisma database connectors with activated migrations . 注意:以上内容适用于已激活迁移的 Prisma数据库连接器。 If your connector doesn't do migrations, different approaches to represent relations are supported. 如果您的连接器不进行迁移,则支持表示关系的其他方法。 The respective datamodel to support this can be generated by introspecting your existing database schema. 可以通过对现有数据库模式进行内部检查来生成支持此操作的各个数据模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM