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
Related
I had a mongoose model with a property attachments which was a string array. I have changed it's type to an object array with keys url and type.
The newly created records are working fine but when I retrieve the old records with string array as type using Model.findOne({}), the results are in a weird format. The string values are converted to object array with position of the letter as key.
But in the db, the values are still in string array format
Any idea what's happening here?
So, I was creating a graphql schema,
type Details {
1: User!
2: User!
3: User!
id: String!
item: String!
}
What I want is to use the Int (1, 2, 3) in my Schema but not sure how.
Someone must have been in the same situation before, help me figure it out.
Names in GraphQL can include numbers, but they cannot start with a number.
If you need to map over an existing API that returns numbers as fields, you can do a mapping such as this one: Graphql: How to map fields that start with number
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
}
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
I'm working with node.js and mongoose. I'm creating a REST API to expose my User model:
var userSchema = new Schema({
_id: {type:Number},
username: {type:String},
age: {type:Number},
genre:{type: Number,ref:'Genre'},
country: {type: Number,ref:'Country'}
});
As you can see I decided to include an _id field, so if I want to create a new user I'll need to generate the value for this field, for example:
exports.createUser = function(req,res){
var user = new User({
_id: //Generate and assing value here
//Other properties are retrieved from the request object
});
};
How could I "generate" or assign a value to my _id field properly? How does mongo deals with this?
I never used mongoose. but if _id is not included in insert query, mongodb driver will generate _ids for you as an ObjectId object. and if you wish to use your own _ids, it's up to you to decide about its type and length, and also you have to guarantee its uniqueness among the collection because any attempt to insert a document with a duplicated _id will fail.
accepted answer of this question may be useful, if you are looking for a method for creating custom _ids that provides a decent degree of guaranteed uniqueness.
mongoDB requires that _id, if supplied, be unique. If _id is not supplied, it is created by the client-side driver (i.e. NOT the mongod server!) as a 12-byte BSON ObjectId with the following structure:
4-byte value representing the seconds since the Unix epoch,
3-byte machine identifier,
2-byte process id, and
3-byte counter, starting with a random value.
more info available here: http://docs.mongodb.org/manual/reference/object-id