错误:零长度定界标识符位于“”或“附近”。第1行:从“区域”中删除在“区域”中。 = $ 1 [英] ERROR: zero-length delimited identifier at or near """" LINE 1: DELETE FROM "regions" WHERE "regions"."" = $1
问题描述
我将Rails 4.1和Postgresql(带有PG gem)用作数据库。从公司到省份,我有很多对很多的协会,有一个名为region的联接表。现在很明显regions表没有主键,因为我使用了{:id => false}。但是,当我尝试使用dependency destroy或仅对自身的区域对象调用destroy时,我会收到此错误:
I'm using Rails 4.1 and Postgresql (with PG gem) as my database. I have a very stand many to many association from companies to provinces with a join table called regions. Now obviously the regions table has no primary key cause I used { :id => false }. But when I try to use depending destroy or just simply calling destroy on the region object it self I get this error:
ERROR: zero-length delimited identifier at or near """"
LINE 1: DELETE FROM "regions" WHERE "regions"."" = $1
我知道问题是由于缺少Regions表的主键引起的。奇怪的是,如果我将主键添加回表中,destroy可以正常工作并且没有错误。但是,如果我从表中删除主键,则会返回错误。我知道这与postgres有关,但是我不知道如何解决这个问题而不必在我的regions表中添加主键列。
I know the problem is caused due to the lack of a primary key for the regions table. And oddly if I add the primary key back to the table destroy works fine and no error. However, if I remove the primary key from the table the error comes back. I know this has something to do with the postgres but I've no idea how to solve this without having to add a primary key column to my regions table.
这里是实际查询
[DEBUG] [AdminUser Load (0.4ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = 1 ORDER BY "admin_users"."id" ASC LIMIT 1] (pid:29655)
[DEBUG] [Province Load (0.2ms) SELECT "provinces".* FROM "provinces" WHERE "provinces"."id" = $1 LIMIT 1 [["id", 5]]] (pid:29655)
[DEBUG] [ (0.1ms) BEGIN] (pid:29655)
[DEBUG] [Region Load (0.3ms) SELECT "regions".* FROM "regions" WHERE "regions"."province_id" = $1 [["province_id", 5]]] (pid:29655)
[ERROR] [PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: DELETE FROM "regions" WHERE "regions"."" = $1
推荐答案
您希望单引号不要在空字符串周围加上双引号,双引号必须定界标识符,而不是有意义的标识符。
You want single quotes not double quotes around empty strings, double quotes make delimited identifiers, and "" isn't a meaningful identifier.
尝试:
WHERE 'regions'.'' = $1
或至少:
WHERE "regions".'' = $1
这篇关于错误:零长度定界标识符位于“”或“附近”。第1行:从“区域”中删除在“区域”中。 = $ 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!