In the database, create a unique or primary key around the field you want to be unique. When you attempt to insert a duplicate, you'll get a duplicate key error. Catch this in your application and handle appropriately.
So never check (only) upfront, its subject to race conditions.
There is only one safe and reliable method for duplication detection - it is to perform this detection on the SQL server side.
You must create unique index by the column or expression which must be unique in the table structure on the SQL server where the value is stored in, and define all columns which are included into this expression as non-nullable. Or you may create primary key by this column/expression - PK creation creates unique index and defines all mentioned columns as non-nullable automatically. Now you can simply insert - if the data to be inserted is already present then the SQL server won't insert the data and will generate unique constraint violation error, which should be detected by your application.
Your possible solution(s) tries to perform the uniqueness check on the client side - this guarantees the uniqueness in concurrent environ only when you lock the table for read and write exclusively before the checking and release this lock only after insertion (of course if uniqueness checking was successful), but this lock may degrade the performance, even dramatically.
This is optional as you can opt only to check server-side when creating a user; however, the user experience would not be as good as checking on both the client and server.
For server-side validation, you must set a unique constraint on the database to ensure uniqueness. For security, it is best practice to validate all data server-side.
Then you pass the error message to your client so you can give feedback to the user. Here is a random example on how to accomplish this in React with Yup.
Planetscale doesn't charge until you hit 1B row reads. I wouldn't worry about the cost for now and opt for client-side validation because this is for the signup page; user experience and reducing friction are really important here.
发布评论
评论(3)
在数据库中,在您想要成为唯一的字段周围创建一个唯一或主键。当您尝试插入副本时,您将获得重复的密钥错误。在您的应用程序中捕获此问题并适当处理。
因此,切勿(仅)预先检查(仅)符合种族条件。
In the database, create a unique or primary key around the field you want to be unique. When you attempt to insert a duplicate, you'll get a duplicate key error. Catch this in your application and handle appropriately.
So never check (only) upfront, its subject to race conditions.
复制检测只有一种安全可靠的方法 - 它是在SQL Server端执行此检测。
您必须通过列或表达式创建唯一的索引,该索引必须在存储该值的SQL Server上的表结构中唯一,并定义所有包含在此表达式中的列,为不可删除。或者,您可以通过此列/表达式创建主键-PK Creation创建唯一的索引,并将所有提到的列自动定义为不可删除的列。现在,您可以简单地插入 - 如果已经存在要插入的数据,则SQL Server不会插入数据并会生成唯一的约束违规错误,该错误应由您的应用程序检测到。
您可能的解决方案试图在客户端执行唯一性检查 - 仅当您锁定表格以在检查和写入之前,仅在插入后才释放此锁时,可以保证并发环境中的唯一性(当然,如果唯一的话检查成功了),但是这个锁可能会降低性能,甚至会极大地降低性能。
There is only one safe and reliable method for duplication detection - it is to perform this detection on the SQL server side.
You must create unique index by the column or expression which must be unique in the table structure on the SQL server where the value is stored in, and define all columns which are included into this expression as non-nullable. Or you may create primary key by this column/expression - PK creation creates unique index and defines all mentioned columns as non-nullable automatically. Now you can simply insert - if the data to be inserted is already present then the SQL server won't insert the data and will generate unique constraint violation error, which should be detected by your application.
Your possible solution(s) tries to perform the uniqueness check on the client side - this guarantees the uniqueness in concurrent environ only when you lock the table for read and write exclusively before the checking and release this lock only after insertion (of course if uniqueness checking was successful), but this lock may degrade the performance, even dramatically.
您的问题的许多部分都需要大量代码 - 我将概述该过程。
for 实时客户端验证,您需要按照描述并设置API路由进行检查。
这是可选的,因为创建用户时只能选择检查服务器端。但是,用户体验不如检查客户端和服务器。
对于服务器端验证,您必须在数据库上设置唯一约束,以确保唯一性。为了安全性,最好的做法是验证所有数据服务器端。
在prisma中,您使用,您可以通过检查“ nofollow noreferrer”> prismaclientnonkonningnonkonnownnoredrequestErterror
反对 prisma错误代码。
然后,您将错误消息传递给客户端,以便您可以向用户提供反馈。这是随机示例< /a>如何在与YUP的反应中实现这一目标。
Planetscale不收费,直到 您击中了1B行读取。我不必担心现在的成本,而是选择客户端验证,因为这是用于注册页面的;用户体验和减少摩擦在这里确实很重要。
Many parts of your question require a ton of code - I will outline the process.
For real-time client-side validation, you need to do as you described and set up an API route to check against.
This is optional as you can opt only to check server-side when creating a user; however, the user experience would not be as good as checking on both the client and server.
For server-side validation, you must set a unique constraint on the database to ensure uniqueness. For security, it is best practice to validate all data server-side.
In Prisma, you use the @@unique decorator, and you catch the unique constraint validation error in the API by checking PrismaClientKnownRequestError
against the Prisma error codes.
Then you pass the error message to your client so you can give feedback to the user. Here is a random example on how to accomplish this in React with Yup.
Planetscale doesn't charge until you hit 1B row reads. I wouldn't worry about the cost for now and opt for client-side validation because this is for the signup page; user experience and reducing friction are really important here.