postgresql 错误:第 1 行或附近的零长度分隔标识符:从“区域”中删除“区域”。”” = $1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23165282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:40:24  来源:igfitidea点击:

ERROR: zero-length delimited identifier at or near """" LINE 1: DELETE FROM "regions" WHERE "regions"."" = $1

postgresqlruby-on-rails-4

提问by Steve

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:

我使用 Rails 4.1 和 Postgresql(带有 PG gem)作为我的数据库。我有一个从公司到省的多对多协会,有一个名为“地区”的联接表。现在显然区域表没有主键,因为我使用了 {:id => false }。但是,当我尝试使用依赖 destroy 或只是简单地在区域对象上调用 destroy 时,我收到此错误:

 ERROR:  zero-length delimited identifier at or near """"
 LINE 1: DELETE FROM "regions" WHERE "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.

我知道问题是由于区域表缺少主键引起的。奇怪的是,如果我将主键添加回表,destroy 工作正常并且没有错误。但是,如果我从表中删除主键,错误又会回来。我知道这与 postgres 有关,但我不知道如何在不必向我的区域表中添加主键列的情况下解决这个问题。

Here is the actual query

这是实际查询

[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" =  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" =   [["province_id", 5]]] (pid:29655)
[ERROR] [PG::SyntaxError: ERROR:  zero-length delimited identifier at or near """"
LINE 1: DELETE FROM "regions" WHERE "regions"."" = 

回答by A-S

You want single quotes not double quotes around empty strings, double quotes make delimited identifiers, and "" isn't a meaningful identifier.

您需要单引号而不是空字符串周围的双引号,双引号构成分隔标识符,而 "" 不是有意义的标识符。

try:

尝试:

WHERE 'regions'.'' = 

or at least:

或者至少:

WHERE "regions".'' = 

回答by Alex Carroll

Try setting dependent: :delete_allinstead.

尝试设置dependent: :delete_all

Example (Not entirely sure how you have your many-to-many relationships setup).

示例(不完全确定您如何设置多对多关系)。

# models/region.rb
...
has_many :provinces_regions, dependent: :delete_all
has_many :provinces, through: :provinces_regions
...

#models/region.rb
...
has_many :provinces_regions, 依赖: :delete_all
has_many :provinces, through: :provinces_regions
...

:destroy/:destroy_allwill remove associated objects by calling their destroy method, and thus callbacks (:before_destroy, :after_destroy, etc.)

:destroy/:destroy_all将通过调用它们的 destroy 方法删除关联的对象,因此回调(:before_destroy、:after_destroy 等)

:delete/:delete_allwill remove associated objects without calling their destroy method.

:delete/:delete_all将删除关联的对象而不调用它们的 destroy 方法。

回答by max pleaner

As none of the answers here worked for me, I thought I'd include my custom fix. Not sure what the translation for your use case is, but I hope the gist of my strategy is clear.

由于这里没有任何答案对我有用,我想我会包含我的自定义修复程序。不确定您的用例的翻译是什么,但我希望我的策略要点是明确的。

I, too, am using a custom primary key (uuid). In order to delete the record on my join table, I skipped active record for the actual delete call and used SQL instead.

我也使用自定义主键 (uuid)。为了删除连接表上的记录,我跳过了实际删除调用的活动记录,而是使用了 SQL。

uid = BucketListPhoto.find_by(bucket_list_id: 12, photo_id: 4).uid

deleted_uids = ActiveRecord::Base.connection.execute(
    "DELETE FROM bucket_list_photos WHERE uid='#{uid}' RETURNING uid"
).to_a.map { |record| record['uid'] }

I overwrote BucketListPhoto#destroyto use this.

我改写BucketListPhoto#destroy使用这个。

I tried to overwrite BuckerListPhoto::ActiveRecord::Query#destroy_allas well but wasn't able to successfully pass an array of uids (to delete many with a single query).

我也尝试覆盖BuckerListPhoto::ActiveRecord::Query#destroy_all,但无法成功传递一组 uid(用单个查询删除多个)。

回答by RWDJ

Based on Alex Carol's answer, but completely different solution.

基于 Alex Carol 的回答,但完全不同的解决方案。

If someone did:

如果有人这样做:

# models/region.rb
...
has_many :provinces_regions, dependent: :destroy_all
has_many :provinces, through: :provinces_regions
...

and are getting this issue, then I would guess you probably did this:

并且遇到这个问题,那么我猜你可能是这样做的:

create_table :provinces_regions, id: false do |t|
  t.belongs_to :regions, index: true
  t.belongs_to :provinces, index: true
end

If the above is true, then Rails is trying to use the ids to run callbacks before destroying. So, give it an id:

如果上述情况属实,那么 Rails 试图id在销毁之前使用s 运行回调。所以,给它一个id

create_table :provinces_regions do |t|
  t.belongs_to :regions, index: true
  t.belongs_to :provinces, index: true
end

And thus you can continue to use dependent: :destroy_alland use callbacks and other features that require the id.

因此,您可以继续使用dependent: :destroy_all和使用回调和其他需要id.

At this stage, you need to make a new migration to correct it. So, as Hussein refers, you should make the migration:

在这个阶段,您需要进行新的迁移来纠正它。因此,正如侯赛因所说,您应该进行迁移:

add_column :provinces_regions, :id, :primary_key

See herefor more details.

请参阅此处了解更多详情。

回答by raphavini

The problem is "" change para ''

问题是“”更改段落''

from:

从:

DELETE FROM "regions" WHERE "regions"."" = 

to:

到:

DELETE FROM "regions" WHERE "regions".'' =