简体   繁体   中英

How to get data from MySql relation table in Prisma

In 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.

It creates a "Ride", "Passenger", "Report" and "Origin" table in MySql. 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.

Lack of relation establishment in "Ride" table, I couldn't get the details from Passenger, Origin and Destination tables when I query "rides()". Is this the correct way to define the 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.

You don't have to change anything on the SQL level to connect the relations afterwards.

Note: The above applies to Prisma database connectors with activated migrations . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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