gpt4 book ai didi

ruby-on-rails - 销毁对象时清空列名

转载 作者:行者123 更新时间:2023-11-29 11:29:32 24 4
gpt4 key购买 nike

我有两个模型:Show 和 Deal。

class Show < ActiveRecord::Base
has_many :deals, :inverse_of => :show, :dependent => :destroy
...

class Deal < ActiveRecord::Base
belongs_to :show, :inverse_of => :deals
...

当我尝试销毁 Show 时出现此错误:

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

为什么列名是空的?在 schema.rb 中:

create_table "deals", :id => false, :force => true do |t|
t.integer "discount_id"
t.integer "show_id"
end

create_table "shows", :force => true do |t|
t.integer "movie_id"
t.integer "hall_id"
t.datetime "show_time"
t.integer "city_id"
t.integer "price"
end

外键添加到数据库

CONSTRAINT fk_deals_shows FOREIGN KEY (show_id)
REFERENCES shows (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION

P.S. 我通过向 deals 表添加主键解决了这个问题,但我并不真的需要它。所以这个问题仍然是实际的。我可以在没有 id 主键的情况下对模型使用依赖项吗?

最佳答案

根据 compositekeys rails 不支持复合主键(这是你的情况)。因此,解决方案之一是使用 has_and_belongs_to_many,因为您的表看起来只是多对多表。

另一种解决方案是使用位于上面链接中的 gem。

关于ruby-on-rails - 销毁对象时清空列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10383995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com