Prisma A link table must not specify any additional scalar fields - datamodel

I have a many to many relation between two tables:
type Post {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
users: [Profile!] #relation(link: TABLE, name: "ParticipatedUsers")
}
type Profile {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
participatingInPosts: [Post!] #relation(name: "ParticipatedUsers")
}
type ParticipatedUsers #relationTable {
post: Post
profile: Profile
requiredPostsCount: Int
publishedPostsCount: Int
}
and when trying to run the command prisma deploy i got the following errors:
Errors:
ParticipatedUsers
✖ A link table must not specify any additional scalar fields.
✖ A link table must not specify any additional scalar fields.
✖ A link table must not specify any additional scalar fields.
how could i create a relation table and add a new fields to that table?
Thanks,

In Prisma you can do this by adding a type with scalar fields and two relations to the types you want to connect, e.g.:
type Post {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
users: [ParticipatedUser!] #relation(link: TABLE, name: "ParticipatedUserPosts")
}
type Profile {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
name: String!
participatingInPosts: [ParticipatedUser!] #relation(link: TABLE, name: "ParticipatedUserProfiles")
}
type ParticipatedUser {
post: Post! #relation(name: "ParticipatedUserPosts")
profile: Profile! #relation(name: "ParticipatedUserProfiles")
requiredPostsCount: Int
publishedPostsCount: Int
}

Related

AWS Amplify GraphQL Schema is causing an error

I am new to Amplify and I am trying to set up a fairly complex schema. When I run amplify push, the only response that I get is ...
An error occurred when pushing the resources to the cloud
This happens after the notice that it will create the resources and I am asked if I want to continue. I deleted most of the schema and tried again with just one model and it worked, so there is a problem somewhere in my schema ... I guess? I don't see anywhere to check it.
Here is the schema
type Article
#model
#key(name: "bySource", fields: ["sourceId", "dateWritten"]) {
id: ID!
link: AWSURL!
title: String!
dateWritten: String!
createdAt: String!
data: AWSJSON!
approved: Boolean!
admin: Boolean!
creatorId: ID!
creator: User #connection(fields: ["creatorId"])
sourceId: ID!
source: Source #connection(fields: ["sourceId"])
tagArtCons: [TagArtCon]
#connection(keyName: "byArticle", fields: ["articleId"])
}
type Tag #model #key(name: "byFrontPage", fields: ["frontpage"]) {
id: ID!
name: String!
createdAt: String!
creatorId: ID!
data: AWSJSON!
frontpage: String
official: Boolean!
tagArtConns: [TagArtCon] #connection(keyName: "byTag", fields: ["tagId"])
}
type TagArtCon
#model
#key(name: "byTag", fields: ["tagId"])
#key(name: "byArticle", fields: ["articleId"]) {
id: ID!
tagId: ID!
articleId: ID!
creatorId: ID!
createdAt: String!
article: Article #connection(fields: ["articleId"])
tag: Tag #connection(fields: ["tagId"])
parentRelations: [TagRelation]
#connection(keyName: "byParent", fields: ["tagId"])
childRelations: [TagRelation]
#connection(keyName: "byChild", fields: ["tagId"])
}
type TagRelation
#model
#key(name: "byParent", fields: ["parentId"])
#key(name: "byChild", fields: ["childId"]) {
id: ID!
parentId: ID!
childId: ID!
creatorId: ID!
createdAt: String!
parentTag: Tag #connection(fields: ["parentId"])
childTag: Tag #connection(fields: ["childId"])
}
type Source #model {
id: ID!
sourceName: String!
sourceUrl: String!
sourceImage: String!
creatorId: ID!
articles: [Article] #connection(keyName: "bySource", fields: ["id"])
}
type User #model {
id: ID!
userName: String!
userImage: String!
admin: Boolean!
createdAt: String!
data: AWSJSON
}
What I am trying to do is have a bunch of articles and a bunch of tags. The tags represent categories, people, etc. I have the following tables
articles,
tags,
a table where each entry ties an article to a tag
a table of relations between tags where parent/child relationships are held
a source table that just holds data for the sources of the articles
a user table
I've made a mistake somewhere and the schema isn't working and it isn't telling me why. I'm gonna keep simplifying this until I can hope to nail down the problem, but I would really appreciate any help as I am new to this. Thanks.
Finally found the problem(s). There is a place where I try to use "articleId" and a place where I try to use "tagId" and both should be "id".

GraphQL - Model(s) belong to specific user field

I'm using Lighthouse package to implement GraphQL, the users in my app belong to an entity and I need to get models that belong to the entity of each user, my schema is like this for the moment
"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date #scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")
"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime #scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")
type Query {
me: User #auth
execution: [RobotTime!]! #all
}
type RobotTime {
id: ID!
instance: Instance!
robot: Robot!
start: DateTime!
end: DateTime!
}
type Instance {
id: ID!
name: String!
token: String!
}
type Robot {
id: ID!
start_bot: String!
process: Process!
}
type Process {
id: ID!
name: String!
token: String!
}
type User {
id: ID!
name: String!
email: String!
created_at: String!
updated_at: String
}
I have searched the documentation, but I can't find anything that helps me to do what I need to do.
Currently I have it done in a controller and I return it as a json with a not so complex logic, there are about 5 models that I use.
Sorry for my bad English
You need to declare the appropriate relation in your User model in laravel, sth like this:
public function entity(): BelongsTo
{
return $this->belongsTo(Entity::class);
}
Then add the relation in User type in graphql:
type User {
id: ID!
name: String!
email: String!
created_at: DateTime!
updated_at: DateTime
entity: Entity #belongsTo
}
Of course, you'll need to declare an Entity model in laravel and type in graphql.

Is it possible to create two or more relations to a model in prisma?

I'm trying to create two relations to a model in datamodel.prisma
datamodel.prisma
type User {
id: ID! #id
user_id: String! #unique
username: String!
email: String! #unique
}
type Operation {
id: ID! #id
teams: [User] #relation(link: INLINE)
created_by: User #relation(link: INLINE)
}
When I try to deploy this is the error I'm getting
Error
Errors:
Operation
✖ The relation field `teams` must specify a `#relation` directive: `#relation(name: "MyRelation")`
✖ The relation field `created_by` must specify a `#relation` directive: `#relation(name: "MyRelation")`
What I want to achieve is an operation can have multiple members(one to many) and can only be created by one member(one to one). How can I achieve this in Prisma?
Could you try creating it like this:
type User {
id: ID! #id
user_id: String! #unique
username: String!
email: String! #unique
}
type Operation {
id: ID! #id
teams: [User] #relation(name: "Teams", link: TABLE)
created_by: User #relation(name: "Createdby", link: TABLE)
}
The name field is required while creating multiple relations to the same model.
Also I'm assuming you are using Postgres.

Filter by Email in prisma GraphQL

I'm trying to filter a user by email, I am using prisma and doing tests with the "playground", is there any way to filter the user's data by email?
My schema:
interface Model {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
type User implements Model {
id: ID! #id
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
username: String
name: String!
surname: String!
email: String!
token_usuario: String! #unique
}
My attempts:
Using prisma 2.0, the db client exposes something like this:
ctx.prisma.user.findMany({
where: {
email: { contains: "something" }
}
});
And I expect the query would follow suit. So try passing in the where field for filtering. Would need to see the full schema to say for sure.

Error: Valid values for the strategy argument of `#scalarList` are: RELATION

Program pops up this -> (Valid values for the strategy argument of #scalarList are: RELATION.) after run prisma deploy. Any one knows why ?
type User {
id: ID! #id
name: String!
email: String! #unique
password: String!
age: Int
img: String
location: Location
hostedEvents: [Event]! #relation(name: "HostedEvents", onDelete: CASCADE)
joinedEvents: [Event]! #relation(name: "EventMembers", onDelete: CASCADE)
pushNotificationTokens: [PushNotificationTokens]!
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
type Event {
id: ID! #id
owner: User! #relation(name: "HostedEvents")
name: String!
imgs: [String]!
description: String
start: DateTime!
end: DateTime!
categories: [Category]!
members: [User]! #relation(name: "EventMembers")
chatRoom: GroupChatRoom!
pendingRequests: [PendingRequest]!
locations: [Location]!
comments: [Comment]!
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
According with docs, when we need to create Fields as Array or List, the directive #scalarlist directive is required, in your case the correct model definition should be with the table/column imgs
type Event {
id: ID! #id
owner: User! #relation(name: "HostedEvents")
name: String!
imgs: [String!]! #scalarList(strategy: RELATION)
description: String
start: DateTime!
end: DateTime!
categories: [Category]!
members: [User]! #relation(name: "EventMembers")
chatRoom: GroupChatRoom!
pendingRequests: [PendingRequest]!
locations: [Location]!
comments: [Comment]!
createdAt: DateTime! #createdAt
updatedAt: DateTime! #updatedAt
}
Docs link -> https://www.prisma.io/docs/datamodel-and-migrations/datamodel-MYSQL-knul/##scalarlist

Resources