Have you tried to solve something like this in pri...
# orm-help
l
Have you tried to solve something like this in prisma? I also created a stack-overflow post on this. https://stackoverflow.com/questions/55000909/auto-generated-incrementing-field-for-prisma
e
Hi there! I don't think it's possible to have auto-generated incrementing field in Prisma. And it tends to be not recommended either (because of asynchronous and stuff).
However, maybe we can't think of alternatives! Why would you need to have incrementing fields ?
l
Thank you for quick reply.
I'm implementing an online store which can process customer orders.
so that with a automatically generated incremental number is more human readable than 25 letter string. so that a customer can refer the order easily with that order number. That feature is requested by the client and It's a must.
e
I see. So I can see 3 alternatives:
1/ When creating a query, do an aggregation query to retrieve the count of orders of a user and increment (but if you can delete orders, it may broke stuff)
2/ When creating an order, do a query for the last created order, get the last number and increment
3/ Create a 6 chars long random string when creating an order (6 chars is easy to remember by a human)
Would one of those alternatives be ok for you?
l
I am thinking about the First two alternative solutions. they seems to be fine. Thanks.
👍 1
What is the best way to get the last created order.? by filtering with the createdAt field?
e
You could orderBy createdAt_desc and take the first 1 result
(I improved my answer in the stack overflow answer: https://stackoverflow.com/a/55122860/10721492 )
a
@LeafyDula: maybe this can inspire some sort of solution: https://www.npmjs.com/package/human-readable-ids
b
nice question. I have the same problem. It could better if we can configure ID like a auto increment Int (like mysql) instead of 25 characters.
e
There currently is an RFC to make auto increment ids happen: https://github.com/prisma/prisma/issues/3403
fast parrot 1