在sequel/ruby 中捕获 postgresql 异常有困难
我有一个具有某种唯一约束的 postgresql 表。
我有 ruby 脚本可以更新这个表。 ruby 脚本应该能够切换到 UPDATE 而不是 INSERT 当出现这种错误时:PGError:错误:重复的键值违反了唯一约束
CMIIW,续集似乎无法捕获此异常?
不管怎样,下面的示例代码是我希望看到的工作,但显然不行:
@myarray.each do |x|
fresh_ds = DB["INSERT INTO mytable (id, col_foo) values ('#{x}' ,'#{myhash[x]}')"]
result = fresh_ds.insert
catch Sequel::Error do
fresh_ds = DB["UPDATE mytable set col_foo = '#{myhash[x]} where id = #{x}"]
result = fresh_ds.update
end
end
也许我的 ruby 代码是错误的,或者我错过了一些我不知道的东西。
有什么解决办法吗?
谢谢。
更新: 下面的代码有效,当违反唯一约束时会捕获错误。
@myarray.each do |x|
begin
# INSERT CODE
rescue Sequel::Error
# UPDATE CODE
end
end
I have postgresql table that has somekind of unique constraint.
I have ruby script that will update this table.
The ruby script should be able to switch to UPDATE instead of INSERT
when this kind of error occured:PGError: ERROR: duplicate key value violates unique constraint
CMIIW, sequel seems cannot catch this exception??
Anyway,sample code below is something that I would like to see to work but apparently not:
@myarray.each do |x|
fresh_ds = DB["INSERT INTO mytable (id, col_foo) values ('#{x}' ,'#{myhash[x]}')"]
result = fresh_ds.insert
catch Sequel::Error do
fresh_ds = DB["UPDATE mytable set col_foo = '#{myhash[x]} where id = #{x}"]
result = fresh_ds.update
end
end
Maybe my ruby code is wrong or I missed something I don't know.
Any solution?
Thanks.
UPDATE:
code below works, the error is caught when unique constraint is violated.
@myarray.each do |x|
begin
# INSERT CODE
rescue Sequel::Error
# UPDATE CODE
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,Ruby 中的 try-catch 像这样:
另一件事是你应该使用 Sequel 的参数插值而不是手动进行整个查询(主要是因为 SQL 注入):
PS:
正如其他人已经指出的那样,还有其他可以说更好的方法来执行“插入或更新”。一般来说,异常是指,呃,异常,即在执行过程中通常不应该发生的情况。您的情况更多是
if
排序,因此我要么首先使用SELECT
检查行是否已存在,或者首先尝试UPDATE
并查看更改的行数,如果为零则执行 INSERT。First, try-catch in Ruby goes like this:
Another thing is that you should use Sequel's parameter interpolation instead of making whole query by hand (mainly because of SQL injection):
PS:
As others already pointed out, there are other, arguably better, ways to perform "insert or update". Generally, exceptions are meant for, er, exceptions, i.e. conditions that shouldn't normally happen during course of execution. Your case is more of
if
sort, so I would either first useSELECT
to check whether row already exists, or, rather, tryUPDATE
first and see the number of changed rows and performINSERT
if it is zero.解决此问题的另一种方法是使用 Sequel::Model 的
find_or_create
方法:An alternate path to solve this problem uses Sequel::Model's
find_or_create
method: