web2py中复合键的最佳实践 [英] Best practice for composite key in web2py

查看:29
本文介绍了web2py中复合键的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

web2py 中复合键的问题是安东尼在这里回答得很好,但我正在寻找答案他在回答中包含了一个警告.他的回答建议在表单验证中使用 primarykeyIS_NOT_IN_DB,警告(和进一步的建议)是:

The question of composite keys in web2py was answered pretty well by Anthony here, but I'm looking for an answer to a caveat he included in his answer. His answer suggested using primarykey and IS_NOT_IN_DB in form validation, and the caveat (and further suggestion) was:

...以上不适用于任意插入表格,但主要用于用户输入提交.

...the above won't work for arbitrary inserts into the table but is primarily intended for user input submissions.

您还可以使用 SQL 在表上设置多列唯一约束(您可以直接在数据库中或通过 web2py .executesql() 方法).但是,即使有这样的约束,您仍然希望在应用程序中进行一些输入验证以避免数据库中的错误.

You can also use SQL to set a multi-column unique constraint on the table (which you can do directly in the database or via the web2py .executesql() method). Even with such a constraint, though, you would still want to do some input validation within your application to avoid errors from the database.

我为字段添加了 CREATE UNIQUE INDEX.我的主要问题是,捕获因这些错误而引发的异常的最佳方法是什么?是否有一个(或几个)地方可以捕获当我尝试 INSERT 在用户表单之外的记录(带有重复键)时 db 层将抛出的异常(哪个会绕过 IS_NOT_IN_DB 检查/技术)?或者我是否必须确保团队知道当他们在这个表上执行任何 INSERT 时,他们必须准备好捕获该异常?或者, ... ?另外,如果我有两个用户试图插入这样的重复项,在 SQLFORM 中是否有一种简单的方法可以让我捕获将被抛出的异常?(对于所有新手问题,抱歉.)

I added the CREATE UNIQUE INDEX for the fields. My main question is, what's the best way to catch the exceptions that will get thrown for these errors? Is there a single (or a few) place(s) where I can catch the exception the db layer will throw when I try to INSERT a record (with a duplicate key) outside of a user form (which would bypass the IS_NOT_IN_DB check/technique)? Or do I have to make sure the team knows that when they do any INSERTs on this table they have to be prepared to catch that exception? Or, ... ? Also, if I have two users trying to insert such duplicates, is there an easy way in SQLFORM for me to catch the exception that will get thrown? (Sorry for all the newbie questions.)

推荐答案

是否有一个(或几个)地方可以捕获异常当我尝试插入记录(带有重复键)?

Is there a single (or a few) place(s) where I can catch the exception the db layer will throw when I try to INSERT a record (with a duplicate key)?

我不这么认为,但您可以编写一个自定义函数来在 try...except(或以其他方式检查重复键)中包装此类插入.

I don't think so, but you could write a custom function to wrap such inserts in a try...except (or otherwise check for duplicate keys).

另外,如果我有两个用户试图插入这样的重复项,是否有一种简单的方法可以让我在 SQLFORM 中捕获将被抛出的异常?

Also, if I have two users trying to insert such duplicates, is there an easy way in SQLFORM for me to catch the exception that will get thrown?

使用 SQLFORM 时,验证器应该捕获重复提交尝试并在表单上返回错误(对于第二个用户).

When using SQLFORM, the validator should catch the duplicate submission attempt and return an error on the form (for the second user).

这篇关于web2py中复合键的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆