mysql2 destroy error - activerecord

I'm trying to destroy an object using a call similar to
MyObject.destroy_all({:user_id => current_user.id, :item_type_id => params[:type_id], :item_id => params[:item_id]})
Rails generates this as a SQL commands:
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
MyObject Load (0.2ms) SELECT `my_objects`.* FROM `my_objects` WHERE `my_objects`.`user_id` = 1 AND `my_objects`.`item_type_id` = 3 AND `my_objects`.`item_id` = 9
(0.1ms) BEGIN
SQL (0.4ms) DELETE FROM `my_objects` WHERE `my_objects`.`` = NULL
The last SQL Statement causes this error (which makes sense)
Mysql2::Error: Unknown column 'my_objects.' in 'where clause': DELETE FROM `my_objects` WHERE `my_objects`.`` = NULL
Am I doing something wrong?
Rails 3.2.1
mysql2 0.3.11
mysql 5 (I think)

Related

How to fix ORA-12801 error in SQL Developer when display tables in connection browser

Using SQL Developer 18.4 MacOSX, I'm getting this error
ORA-12801: error signaled in parallel query server P000
ORA-01722:
invalid number
When I try to display the tables list in the connections tab.
I've isolated the Query.
When I execute each part of the "union all" one by one, there is no error.
when I execute the all query you got the error.
When I disable the parallel option it works fine (ALTER SESSION disable PARALLEL query).
select * from (
SELECT o.OBJECT_NAME, o.OBJECT_ID ,'' short_name, NULL partitioned,
o.sharded,
case when o.sharded <> 'Y' then o.duplicated else 'N' end duplicated,
NULL iot_type,
o.OWNER OBJECT_OWNER, o.CREATED, o.LAST_DDL_TIME, O.GENERATED, O.TEMPORARY, NULL EXTERNAL
FROM SYS.Dba_OBJECTS O
WHERE O.OWNER = :SCHEMA
AND O.OBJECT_TYPE = 'TABLE'
union all
SELECT OBJECT_NAME, OBJECT_ID , syn.SYNONYM_NAME short_NAME, NULL partitioned,
o.sharded,
case when o.sharded <> 'Y' then o.duplicated else 'N' end duplicated,
NULL iot_type,
SYN.TABLE_OWNER OBJECT_OWNER, o.CREATED, o.LAST_DDL_TIME, O.GENERATED, O.TEMPORARY, NULL EXTERNAL
FROM SYS.Dba_OBJECTS O, sys.user_synonyms syn
WHERE syn.table_owner = o.owner
and syn.TABLE_NAME = o.object_NAME
and o.object_type = 'TABLE'
and :INCLUDE_SYNS = 1
)

postgres, database with schema in ruby code

I connected to postgres database from ruby with no problem, but when they added schema , I got confused and go error , here is the code I am trying to run :
require 'pg'
#pg_conn = PGconn.connect("xxxxxxx.us-gov-west-1.rds.amazonaws.com", 5432, '', '', "BRCArchive", "yyyy", "zzzz")
count = #pg_conn.exec('SELECT COUNT(*) FROM "brcmanager.Agency"')
puts count
I got this error :
Called from brc_migration2.rb:7:in `<main>'
brc_migration2.rb:9:in `exec': ERROR: relation "brcmanager.Agency" does not exist (PG::UndefinedTable)
LINE 1: SELECT COUNT(*) FROM "brcmanager.Agency"
^
from brc_migration2.rb:9:in `<main>'
Thanks,
#pg_conn.exec("set search_path=brcmanager;")
and then
count = #pg_conn.exec('SELECT COUNT(*) FROM Agency')
The error tells you what's wrong. The table brcmanager.Agency does not exist in that database, perhaps you misspelled it?
You can see what tables exist in that database by running this SQL:
SELECT * FROM information_schema.tables WHERE table_schema = 'information_schema'

How do I get the results of a group_and_count using the Sequel gem?

I have a Sequel-based class that I need to do some summaries on.
I'm doing a group_and_count, and I can see that it's generating the right query. However, when I try to access the results, Sequel is trying to coerce the rows into the class I'm accessing through:
[33] pry(main)> grouped = Pancakes::Stack.active.group_and_count('health_state')
=> #<Sequel::Mysql2::Dataset: "SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state'">
[34] pry(main)> grouped.each_entry { |row| puts row }
I sequel: (0.001344s) SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state'
#<Pancakes::Stack:0x000000089251a0>
=> #<Sequel::Mysql2::Dataset: "SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state'">
[35] pry(main)> grouped.first
I sequel: (0.001502s) SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state' LIMIT 1
I sequel: (0.001243s) SELECT * FROM `pancakes_stacks` WHERE (`id` IS NULL) LIMIT 1
=> #<Pancakes::Stack:0x44b068c>
I can get what I need by working around the ORM stuff, but that appears to require me to re-implement the active method above, and figure out how to get the table name from the class name:
[38] pry(main)> groupie = grouped.db[:pancakes_stacks].where(deleted_at:nil).group_and_count(:health_state)
=> #<Sequel::Mysql2::Dataset: "SELECT `health_state`, count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY `health_state`">
[39] pry(main)> groupie.each_entry { |row| puts row }
I sequel: (0.001598s) SELECT `health_state`, count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY `health_state`
{:health_state=>nil, :count=>3}
{:health_state=>"healthy", :count=>10}
Isn't there an easier way? I've spent a lot of time on the querying page, but none of the examples show how to access the results.
You probably just want to add a .naked to your dataset, which will make the dataset return hashes instead of model objects.
I personally use the square brackets method to get the :count attribute for a row:
row[:count]

ActiveRecord - Get the last n records and delete them in one command?

Hello all and thanks for taking the time to answer my question.
The question is really explained in the title.
I tried Model.last(n).destroy_all but none of that would work.
I was wondering if it is possible to do it in one line, and if not what would be the cleanest way of doing it?
Thanks again!
To do it in one SQL query use delete_all:
Model.order(created_at: :desc).limit(n).delete_all
But delete_all won't execute any model callbacks or validations
To run callbacks and validations use destroy_all:
Model.order(created_at: :desc).limit(n).destroy_all
Unfortunately destroy_all will execute n + 1 SQL queries: 1 query to retrieve records and n queries to delete each record.
You can achieve this by:
Model.last(n).each(&:destroy)
#IvanDenisov points out another way to do this:
Model.order('created_at DESC').limit(n).destroy_all
It's basically doing the same thing according to Rails API Doc, but a little bit verbose. Besides, it doesn't do all things in one SQL query.
Detailed comparison of SQL queries
I tried to run both codes in rails console under Ruby 2.0.0p253 && Rails 4.0.4, here are the results:
2.0.0p353 :002 > Role.last(3).each(&:destroy)
Role Load (1.0ms) SELECT "roles".* FROM "roles" ORDER BY "roles"."id" DESC LIMIT 3
(0.3ms) BEGIN
SQL (3.5ms) DELETE FROM "roles" WHERE "roles"."id" = $1 [["id", 5487]]
(11.8ms) COMMIT
(0.1ms) BEGIN
SQL (0.2ms) DELETE FROM "roles" WHERE "roles"."id" = $1 [["id", 5488]]
(5.4ms) COMMIT
(0.1ms) BEGIN
SQL (0.2ms) DELETE FROM "roles" WHERE "roles"."id" = $1 [["id", 5489]]
(4.6ms) COMMIT
2.0.0p353 :004 > Role.order('created_at DESC').limit(3).destroy_all
Role Load (0.9ms) SELECT "roles".* FROM "roles" ORDER BY created_at DESC LIMIT 3
(0.2ms) BEGIN
SQL (0.2ms) DELETE FROM "roles" WHERE "roles"."id" = $1 [["id", 5492]]
(6.6ms) COMMIT
(0.2ms) BEGIN
SQL (0.2ms) DELETE FROM "roles" WHERE "roles"."id" = $1 [["id", 5491]]
(0.4ms) COMMIT
(0.1ms) BEGIN
SQL (0.1ms) DELETE FROM "roles" WHERE "roles"."id" = $1 [["id", 5490]]
(0.2ms) COMMIT
The DELETE parts are exactly the same. They both took multiple SQL queries.
The only difference is SELECT part, if we change 'created_at DESC' to 'id DESC', they will be exactly the same too.

ActiveRecord unknown column

I ran into a little problem I have a has_many through relationship here is the code for the models
class User < ActiveRecord::Base
has_many :friendships
has_many :followings, :through => :friendships, :foreign_key => "followed_id"
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :following, :class_name => "User", :foreign_key => "followed_id"
end
now at the console I can type u = User.first and then u.friendships.first.following this gives me the first user u is following, but when I type u.friendships.last.following I get this error
the SELECT statement from u.friendships.first.following
Friendship Load (0.3ms) SELECT `friendships`.* FROM `friendships` WHERE `friendships`.`user_id` = 208 LIMIT 1
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 209 LIMIT 1
and the SELECT statement from u.friendships.last.following
Friendship Load (0.3ms) SELECT `friendships`.* FROM `friendships` WHERE `friendships`.`user_id` = 208 ORDER BY `friendships`.`` DESC LIMIT 1
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'friendships.' in 'order
clause': SELECT `friendships`.* FROM `friendships` WHERE `friendships`.`user_id` = 208
ORDER BY `friendships`.`` DESC LIMIT 1
if I then run u.friendships and then u.friendships.last.following again, I don't get the error anymore, why is that?
Heres my sql output for friendships, straight from your code on Rails 3.2.9 / postgresql:
# u.friendships.first.following
Friendship Load (0.9ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = 1 LIMIT 1
# u.friendships.first.following
Friendship Load (1.3ms) SELECT "friendships".* FROM "friendships" WHERE "friendships"."user_id" = 1 ORDER BY "friendships"."id" DESC LIMIT 1
So for some reason for me, id is getting picked up automatically in ORDER BY "friendships"."id" and it works. Maybe your problem has something to do with your DB?
#Statements used to create the db for reproducing this problem
CREATE TABLE users (id SERIAL PRIMARY KEY)
CREATE TABLE friendships (
id SERIAL PRIMARY KEY,
user_id integer
followed_id integer
);

Resources