'How to get Many to Many relation data?
I have a NodeJS app that uses PostgresQL and I'm trying to retrieve some relational data:
I have a Movie table which has a relation with my User table:
// prisma.schema
model Movie {
id Int @id @default(autoincrement())
original_title String @unique
tmdb_id Int @unique
poster_path String?
backdrop_path String?
release_date String?
runtime Int?
tagline String?
overview String?
users User[]
notification Notification[]
movieRating MovieRating[]
}
model User {
id Int @id @default(autoincrement())
email String @unique
user_name String @unique
password String
movies Movie[]
notifications Notification[]
followedBy User[] @relation("UserFollows", references: [id])
following User[] @relation("UserFollows", references: [id])
notification Notification[] @relation("FollowedUser")
movieRating MovieRating[]
resetToken String? @unique
activationToken String? @unique
}
In my back-end I want to select all users that have a relation to a movie: (17 is the id of a movie)
const users = await pool.query('SELECT id, user FROM "Movie" WHERE id = $1', [17]);
I would expect an array of id's from the table user from table movie row 17. But the actual result is:
Result {
command: 'SELECT',
rowCount: 1,
oid: null,
rows: [ { id: 17, user: 'db_user' } ],
fields: [
Field {
name: 'id',
tableID: 189397,
columnID: 1,
dataTypeID: 23,
dataTypeSize: 4,
dataTypeModifier: -1,
format: 'text'
},
Field {
name: 'user',
tableID: 0,
columnID: 0,
dataTypeID: 19,
dataTypeSize: 64,
dataTypeModifier: -1,
format: 'text'
}
],
_parsers: [ [Function: parseInteger], [Function: noParse] ],
_types: TypeOverrides {
_types: {
getTypeParser: [Function: getTypeParser],
setTypeParser: [Function: setTypeParser],
arrayParser: [Object],
builtins: [Object]
},
text: {},
binary: {}
},
RowCtor: null,
rowAsArray: false
}
Something is happening but definitely not what I expected. db_user is the user name of my database...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
