我在 Ruby on Rails 上使用 ActiveRecord 和 oracle 适配器。尝试删除行时出现 StatementInvalid Exception
。
这是我的 table 的样子:room_user_table
room | user
1010 | a
1010 | b
1011 | a
1011 | c
1011 | d
我的 ruby ActiveRecord 类:
class RoomUserTable < ActiveRecord:Base
self.table_name = 'room_user_table'
end
例如,现在我想删除第二行,所以我发出
RoomUserTable.destroy_all(:room => 1010, :user => 'b')
但这是抛出 ActiveRecord::StatementInvalid 异常
OCIError: ORA-01741: illegal zero-length identifier: DELETE FROM "ROOM_USER_TABLE" WHERE "ROOM_USER_TABLE"."" = :a1
如有任何帮助,我们将不胜感激。
我的测试 Controller .rb
class TestController < ActionController::Base
def test
RoomUserTable.destroy_all(:room => 1010, :user => 'b')
end
end
最佳答案
您的 RoomUserTable
没有主键,这导致它运行您在问题 WHERE "ROOM_USER_TABLE".""= ...
这反过来又导致 Oracle 抛出一个不稳定的问题。 Rails 模型需要有主键。
该表看起来像一个连接表,因此您根本不需要为其创建模型或直接查询它。您可以在 User
和 Room
之间使用 has_and_belongs_to_many
关系并指定连接表。
class Room < ActiveRecord::Base
has_and_belongs_to_many :users, join_table: :room_user_table
end
class User < ActiveRecord::Base
has_and_belongs_to_many :rooms, join_table: :room_user_table
end
或类似的。
编辑 - 已经说过您的用户列中有 a
、b
等,所以我不知道那个表是什么,但问题仍然是同样,您没有主键。
关于ruby-on-rails - ActiveRecord destroy_all 抛出 StatementInvalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17720312/