I have a problem understanding relations and data-modeling in Prisma. I have an "easy" example about two users who can participate in a tennis game. So i have:
Model User {
id Int #id
name String
}
Model Game {
id Int #id
player1 PlayerInGame
player2 PlayerInGame
}
Model PlayerInGame {
id Int #id
player User
game Game
}
It gives me this error:
Error validating model "Game": Ambiguous relation detected. The fields `player1` and `player2` in model `Game` both refer to `PlayerInGame`. Please provide different relation names for them by adding `#relation(<name>).
How can i fix this?
Thanks in advance.
I tried to at a #relation field as well, but that gave me the following error:
model Game {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
player1 PlayerInGame #relation("player1")
player2 PlayerInGame #relation("player2")
}
Error validating model "Game": Automatic related field generation would cause a naming conflict. Please add an explicit opposite relation field.
You can fix this error by providing two relation fields on PlayerInGame. You have two relation fields on Game. Hence you also need two opposite relation fields on PlayerInGame. In your application logic you have to ensure that only one of those two relation fields is set. So only PlayerInGame.gameAsPlayer1 or PlayerInGame.gameAsPlayer2 should be set according to your business domain.
model User {
id Int #id
name String
}
model Game {
id Int #id
player1 PlayerInGame #relation("GamePlayer1")
player2 PlayerInGame #relation("GamePlayer2")
}
model PlayerInGame {
id Int #id
player User
gameAsPlayer1 Game? #relation("GamePlayer1")
gameAsPlayer2 Game? #relation("GamePlayer2")
}
The best way to do it would be a many-to-many relation. You can add as many users to a game as well in the future and will be scalable in turn.
model User {
id Int #id
name String
games Game[]
}
model Game {
id Int #id
name String
users User[]
}
You can then conditionally check the number of players in the game and limit them in your business logic.
In my case, I was having this issue due to a FK being used in multiple fields in the same column. Whenever you have more than 1 relation to a model you need to provide a relation name to disambiguate the relation.
example:
model User {
id Int #id
name String
games Game[]
}
model Game {
game_id Int #id
name String
users User[]
}
model PlayerInGame {
id Int #id
player User
gameAsPlayer1 Game? #relation("GamePlayer1", fields: [gameAsPlayer1Id], references: [game_id])
gameAsPlayer1Id Int
gameAsPlayer2 Game? #relation("GamePlayer2", fields: [gameAsPlayer2Id], references: [game_id])
gameAsPlayer2Id Int
}
Related
can I query limit scalar list query?
Example model:
model Test {
id String #id #default(auto()) #map("_id") #db.ObjectId
results TestResults[]
}
type TestResults{
batch Int
}
Can I do a query that only get 1 results from this scalar list?
Thanks in advance for helping
I expect the response will be one data from this array and not all
Hey guy I am studying and I am trying to solve this hard exercise.
on the above picture I am trying to implement this into a code: however I find it extremely hard. I have tried the following at the moment, don't know if its correct or not, can someone please help me?
just some info that the assignment says:
-The attribute product should be an objekt of the class Product
Shipping adress and billing address should be an object of the class Adress
User should also be an object of the class User
CalculateOrder (should calculate order and sum the price for everything
Calculate vat should add 25 %
printAllorders() should print all orders
what is mean by "object of the class"
what is mean by "object of the class"
Object off the class means you build an instance of it like
Proudct p = new Product();
With this you build an instance or object of the Class Product
Your class lineItem has an Attribute Product.
This means that the attribute is not of an primitive type like int, boolean .... it is of type product
public class LineItem(){
int id;
double price;
int quantity;
Product prod;
}
Is there any way to skip id in when modeling a prisma type?
type User {
myid: string! #unique
}
I have tried this. but is gives, ✖ One field of the typeUsermust be marked as the id field with the#iddirective.
actually I want to customize the prisma id.
I dont want to use the default prisma id which always starts with ck---
I want different patterns of id for different types:
for example:
user id : user---abc---123
product id: product---abc---123
the id in any field is supposed to identify it, customizing it won't give you any advantage if you want a uuid you can always use
model User {
myid: String #default(uuid()) #id
}
or
model User {
myid: Int #default(autoincrement()) #id
}
you can learn more here:
https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model
Im trying to implement a database design using Objection.js but I don't know how to define an inheritance relationship with multiple categories that are mutually exclusive.
The case in question is as follows: I want to implement likes for posts, comments, and replies. As far as I know, "The most extensible solution is to have just one "base" table, and "inherit" all other tables from it." as stated in another SO question. The idea is to link a like to an entity and for an entity to be either a post, comment or another entity.
User
id PK int
name string
Entity
id PK int
Like
userId int Fk >- User.id
entityId int Fk >- Entity.id
Article
id PK int Fk >- Entity.id
author int FK >- User.id
title string
text string
Comment
id PK int Fk >- Entity.id
articleId int FK >- Article.id
authorId int FK >- User.id
text string
--There can be more entities like reply, image, etc
In Objection.js you implement a relationship as follows:
class Entity extends Model {
static tableName = 'entities';
static relationMappings = {
Post: {
relation: Model.HasOneRelation,
modelClass: Like,
join: {
from: 'post.id',
to: 'like.postId'
}
}
}
}
However if a do that I don't know how to define that it can be also comment or reply. Not only Post.
Is there a way to archive this? If not, should I use another JS library? Should I change my schema to make it work with Objection.js?
Relevant links:
Implementing Comments and Likes in database
http://vincit.github.io/objection.js/#relations
I've been looking at Event Store for a little while now and one thing that stumped me is when should one consider writing a projection? Is it common practice to create a flattened projection?
The setup here is using a stream per aggregate
For example, lets say I have 2 events that look like this:
public class OrderPlaced
{
public OrderPlaced(string reference, Currency currency, Amount amount)
{
...
}
public string Reference { get; }
public Currency Currency { get; } //Custom type
public Amount Amount { get; } //Custom type
}
public class OrderCompleted
{
public OrderCompleted(string reference)
{
this.Reference = reference;
}
public string Reference { get; }
}
I have the following query:
fromCategory('MyCategory')
.whenAny(function(s, e) {
linkTo("Foo", e);
})
The above doesn't do a great deal and only aggregates all the streams into a singular. Is it possible to project a view that is more flat, for example into something like this? Perhaps I got my wires crossed but apparently using emit can achieve this?
{
string Reference;
string CurrencyCode;
decimal PayingAmount;
}
My thinking is, once I have written to the stream I can guarantee the aggregate is in a valid state and thus for any interested parties I should only expose the fields these processes require. Is projecting a simple model (a de-nomarlized view) the correct thing to do..
The above is a trivial example, but you can imagine an aggregate being a little more complicated.
If I have missed anything or further clarification is needed then please comment and I can add.
You are looking for a standard event category projection.
It emits linked events to steam that are called ´$ce-´. The category there is your object type.
For example, your aggregate type is Order, and you write events OrderCreated, OrderLineAdded, etc to streams with names Order-1, Order-2, where 1 and 3 are your aggregate root ids. Then, the $ce-Order stream will contain all events for all aggregates of that type.
Have a look at the standard projections documentation.
Usually this is exactly the way to create read-side projections - by creating a catch-up subscription on category streams and updating the read model accordingly.
In order to run projections, you need to use --run-projections=all –-start-standard-projections=true to see it working.