ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in column "id" violates not-null constraint - ruby-on-rails-3.1

I am getting the following error frequently in my application. If I restart my rails application this error will not appear for some time.
Rails - 3.1.3
Ruby - 1.9.3
ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in
column "id" violates not-null constraint
Directory.create(:name=>"name",:alias_name=>"name",:site_id=>2,:type=>"Directory")
(0.3ms) BEGIN
SQL (0.8ms) INSERT INTO "databases" ("alias_name", "created_at", "created_by", "id", "name", "site_id", "type", "updated_at", "updated_by") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) [["alias_name", "name"], ["created_at", Wed, 17 Apr 2013 16:46:25 IST +05:30], ["created_by", nil], ["id", nil], ["name", "name"], ["site_id", 2], ["type", "Directory"], ["updated_at", Wed, 17 Apr 2013 16:46:25 IST +05:30], ["updated_by", nil]]
PG::Error: ERROR: null value in column "id" violates not-null constraint
: INSERT INTO "databases" ("alias_name", "created_at", "created_by", "id", "name", "site_id", "type", "updated_at", "updated_by") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
(0.2ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in column "id" violates not-null constraint
: INSERT INTO "databases" ("alias_name", "created_at", "created_by", "id", "name", "site_id", "type", "updated_at", "updated_by") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1034:in `get_last_result'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1034:in `exec_cache'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:603:in `block in exec_query'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:601:in `exec_query'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/relation.rb:70:in `insert'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/persistence.rb:313:in `create'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/timestamp.rb:51:in `create'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:268:in `block in create'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:390:in `_run_create_callbacks'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
... 7 levels...
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/validations.rb:50:in `save'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:241:in `block in save'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:240:in `save'
from (irb):9
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion#web/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p194 :010 >
I don't know why id is going as nil in the rails generated SQL.
Can anyone help me please !!! I am struggling lot with this error
Thanks in Advance,
Selva Chezhian

I have same problem once. Here is my problem. I did a
rake db:schema:dump
rake db:schema:load
and the table got problem as below:
create_table "campaign_line_items_backup", :id => false, :force => true do |t|
t.integer "id"
t.integer "campaign_id"
t.integer "size_id"
t.text "name"
it should be
create_table "campaign_line_items_backup", :force => true do |t|
t.integer "campaign_id"
t.integer "size_id"
t.text "name"
I am not sure about your problem, That's the way I fixed mine.

The best solution would be retrying Insert operation.

Related

Ruby Sequel with Postgresql: How do I remove reference to an 'undefinedtable'

Hi: I need to remove a reference to a table that is incorrect but I cannot find where that reference is stored.
I am using ruby 2.6.0, sequel 5.30.0 and postgres 13 and Sinatra 2.0.8.
I mistakenly referred to a table with a misspelling in one of my migration files, ran the migrator and now I am getting an error that the table (with the misspelled name) does not exist when I try to run the application.
Sequel::DatabaseError: PG::UndefinedTable: ERROR: relation "renew_event_types" does not exist
I corrected the misspelled reference in my migration (it should be "renewal_event_types"), ran the migrations back to the one before this then reran all the migrations forward and I still get the error. I dropped the database, ran the migrations again and still get the error. I cannot find any file with the word "renew_event_types" in it on my system so I believe that somehow this reference is not in ascii format. If it was stored in Postgres, it should have been removed when I dropped the database. Would it be stored in a sequel file somewhere associated with this project?
Here is the parc-mbr/app/renewalEventType.rb where I initially spelled the reference to the foreign key "renew_event_type_id" then changed it to "renewal_event_type_id" to reflect the correct table "renewal_event_type" (note change from renew to renewal).
require_relative '../config/sequel'
module MemberTracker
class RenewEventType < Sequel::Model
one_to_many :mbr_renewals, :class=>"MemberTracker::MbrRenewal", key: :renewal_event_type_id
many_to_one :auth_users, :class=> "MemberTracker::Auth_user", key: :a_user_id
end
end
Here is the other associated model file parc-mbr/app/mbrRenewal.rb
require_relative '../config/sequel'
module MemberTracker
class MbrRenewal < Sequel::Model
one_to_many :log, :class =>"MemberTracker::Log", key: :mbr_renewal_id
many_to_one :auth_user, :class=>Auth_user, key: :a_user_id
many_to_one :member, :class=>Member, key: :mbr_id
many_to_one :renewalEventType
def getRenewalTypeIDs
DB.from(:renewal_event_types).select(:id, :name).all
end
end
end
Here is the full trace in my terminal. Thanks for any pointers on how to begin to solve this problem.
2022-09-18 16:06:06.135 PDT [1837] ERROR: relation "renew_event_types" does not exist at character 1133
2022-09-18 16:06:06.135 PDT [1837] STATEMENT: SELECT "pg_attribute"."attname" AS "name", CAST("pg_attribute"."atttypid" AS integer) AS "oid", CAST("basetype"."oid" AS integer) AS "base_oid", format_type("basetype"."oid", "pg_type"."typtypmod") AS "db_base_type", format_type("pg_type"."oid", "pg_attribute"."atttypmod") AS "db_type", pg_get_expr("pg_attrdef"."adbin", "pg_class"."oid") AS "default", NOT "pg_attribute"."attnotnull" AS "allow_null", COALESCE(("pg_attribute"."attnum" = ANY("pg_index"."indkey")), false) AS "primary_key", "pg_attribute"."attidentity" FROM "pg_class" INNER JOIN "pg_attribute" ON ("pg_attribute"."attrelid" = "pg_class"."oid") INNER JOIN "pg_type" ON ("pg_type"."oid" = "pg_attribute"."atttypid") LEFT OUTER JOIN "pg_type" AS "basetype" ON ("basetype"."oid" = "pg_type"."typbasetype") LEFT OUTER JOIN "pg_attrdef" ON (("pg_attrdef"."adrelid" = "pg_class"."oid") AND ("pg_attrdef"."adnum" = "pg_attribute"."attnum")) LEFT OUTER JOIN "pg_index" ON (("pg_index"."indrelid" = "pg_class"."oid") AND ("pg_index"."indisprimary" IS TRUE)) WHERE (("pg_attribute"."attisdropped" IS FALSE) AND ("pg_attribute"."attnum" > 0) AND ("pg_class"."oid" = CAST(CAST('"renew_event_types"' AS regclass) AS oid))) ORDER BY "pg_attribute"."attnum"
2022-09-18 16:06:06.135 PDT [1837] ERROR: relation "renew_event_types" does not exist at character 15
2022-09-18 16:06:06.135 PDT [1837] STATEMENT: SELECT * FROM "renew_event_types" LIMIT 1
bundler: failed to load command: rackup (/Users/megazoic/.rbenv/versions/2.6.6/bin/rackup)
Sequel::DatabaseError: PG::UndefinedTable: ERROR: relation "renew_event_types" does not exist
LINE 1: SELECT * FROM "renew_event_types" LIMIT 1
^
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:156:in `exec'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:156:in `block in execute_query'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/database/logging.rb:38:in `log_connection_yield'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:156:in `execute_query'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:144:in `block in execute'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:120:in `check_disconnect_errors'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:144:in `execute'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:500:in `_execute'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:318:in `block (2 levels) in execute'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:522:in `check_database_errors'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:318:in `block in execute'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/connection_pool/threaded.rb:88:in `hold'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/database/connecting.rb:270:in `synchronize'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:318:in `execute'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/dataset/actions.rb:1089:in `execute'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/adapters/postgres.rb:614:in `fetch_rows'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/dataset/actions.rb:152:in `each'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/dataset/actions.rb:86:in `columns!'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/dataset/actions.rb:76:in `columns'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:242:in `columns'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:811:in `block (2 levels) in get_db_schema'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:725:in `block in check_non_connection_error'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/database/transactions.rb:201:in `block in transaction'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/connection_pool/threaded.rb:92:in `hold'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/database/connecting.rb:270:in `synchronize'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/database/transactions.rb:195:in `transaction'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:725:in `check_non_connection_error'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:811:in `block in get_db_schema'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:844:in `get_db_schema'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:644:in `set_dataset'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/sequel-5.30.0/lib/sequel/model/base.rb:499:in `inherited'
/Users/megazoic/Documents/dev/proj/versioned/ruby/parc-mbr/app/renewalEventType.rb:4:in `<module:MemberTracker>'
/Users/megazoic/Documents/dev/proj/versioned/ruby/parc-mbr/app/renewalEventType.rb:3:in `<top (required)>'
/Users/megazoic/Documents/dev/proj/versioned/ruby/parc-mbr/app/api.rb:20:in `require_relative'
/Users/megazoic/Documents/dev/proj/versioned/ruby/parc-mbr/app/api.rb:20:in `<top (required)>'
/Users/megazoic/Documents/dev/proj/versioned/ruby/parc-mbr/config.ru:1:in `require_relative'
/Users/megazoic/Documents/dev/proj/versioned/ruby/parc-mbr/config.ru:1:in `block in <main>'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/builder.rb:116:in `eval'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/builder.rb:116:in `new_from_string'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/builder.rb:105:in `load_file'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/builder.rb:66:in `parse_file'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/server.rb:349:in `build_app_and_options_from_config'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/server.rb:249:in `app'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/server.rb:422:in `wrapped_app'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/server.rb:312:in `block in start'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/server.rb:379:in `handle_profiling'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/server.rb:311:in `start'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/lib/rack/server.rb:168:in `start'
/Users/megazoic/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rack-2.2.2/bin/rackup:5:in `<top (required)>'
/Users/megazoic/.rbenv/versions/2.6.6/bin/rackup:23:in `load'
/Users/megazoic/.rbenv/versions/2.6.6/bin/rackup:23:in `<top (required)>'
My error was to leave the model name unchanged (class RenewEventType) even though I had changed the creation of the associated table in the migration files and the reference to the foreign key in the model description. Ruby and presumably Sequel used this name of the model to look for the corresponding table that hadn't been created. Thanks #spikermann for pointing me in the right direction.

ActiveRecord: no such table for unrelated table, but only on one server

I'm trying to set an attribute in an ActiveRecord table. I have the following file which works on one server, but gives an error on the other.
require 'active_record'
require 'sqlite3'
require_relative 'models/master'
$CONFIG = YAML.load_file('config.yml').freeze
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection($CONFIG[:connection])
s = Stock.find_by(isbn: 1234)
s.status = 'CHECKED_OUT'
s.save!
Both versions have the same database, the same code, etc. through git. However, on the second server I get the following error:
/home/web/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize': SQLite3::SQLException: no such table: main.book: UPDATE "stock" SET "status" = ? WHERE "stock"."ROWID" = ? (ActiveRecord::StatementInvalid)
from /home/web/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'
from /home/web/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:212:in `block (2 levels) in exec_query'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:46:in `block in permit_concurrent_loads'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/concurrency/share_lock.rb:185:in `yield_shares'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies/interlock.rb:45:in `permit_concurrent_loads'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:209:in `block in exec_query'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:612:in `block (2 levels) in log'
from /home/web/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:611:in `block in log'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:603:in `log'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:208:in `exec_query'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:239:in `exec_delete'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:140:in `update'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/query_cache.rb:17:in `update'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/relation.rb:85:in `_update_record'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/persistence.rb:575:in `_update_record'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/locking/optimistic.rb:80:in `_update_record'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/attribute_methods/dirty.rb:292:in `_update_record'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/callbacks.rb:344:in `block in _update_record'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:131:in `run_callbacks'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:827:in `_run_update_callbacks'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/callbacks.rb:344:in `_update_record'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/timestamp.rb:107:in `_update_record'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/persistence.rb:563:in `create_or_update'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/callbacks.rb:336:in `block in create_or_update'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:131:in `run_callbacks'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:827:in `_run_save_callbacks'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/callbacks.rb:336:in `create_or_update'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/persistence.rb:162:in `save!'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/validations.rb:50:in `save!'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/attribute_methods/dirty.rb:43:in `save!'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:313:in `block in save!'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
from /home/web/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:210:in `transaction'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:313:in `save!'
from /home/web/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/suppressor.rb:46:in `save!'
from test.rb:12:in `<main>'
D, [2018-02-04T19:36:39.046660 #26810] DEBUG -- : [1m[36mStock Load (0.2ms)[0m [1m[34mSELECT "stock".* FROM "stock" WHERE "stock"."isbn" = ? LIMIT ?[0m [["isbn", 1234], ["LIMIT", 1]]
D, [2018-02-04T19:36:39.055100 #26810] DEBUG -- : [1m[35m (0.1ms)[0m [1m[36mbegin transaction[0m
D, [2018-02-04T19:36:39.056339 #26810] DEBUG -- : [1m[35mSQL (0.3ms)[0m [1m[33mUPDATE "stock" SET "status" = ? WHERE "stock"."ROWID" = ?[0m [["status", "CHECKED_OUT"], ["ROWID", 1]]
D, [2018-02-04T19:36:39.056522 #26810] DEBUG -- : [1m[35m (0.1ms)[0m [1m[31mrollback transaction[0m
The interesting thing is that the table it says doesn't exist is not used at all in the query (as you can see in the logged queries). The Stock model has a belongs_to :book, foreign_key: 'isbn' which stops the error when removed. However, changing any column other than status does not cause the error.
The servers do have different versions of ActiveRecord and SQLite, however I am more interested in why this behavior is occurring so that I can prevent it in the future. The code works on ActiveRecord 2.6.8, but fails on 2.6.14. Is there any obvious reason why this would happen?

Rails could not find the inverse association in has many relation

I have two models (one to many relationship between them) in rails 5,
One is ScholarshipGroup and Scholarship model with Admin namespace.
ScholarshipGroup Model:
class Admin::ScholarshipGroup < ApplicationRecord
has_many :admin_scholarships, :class_name => 'Admin::Scholarship',inverse_of: :admin_scholarship_group
end
And Scholarship Model:
class Admin::Scholarship < ApplicationRecord
belongs_to :group, :class_name=> 'Admin::ScholarshipGroup', inverse_of: 'admin_scholarships'
end
I want to need data for all scholarships under a ScholarshipGroup.
But when I run this query from rails console:
$ Admin::ScholarshipGroup.first.admin_scholarships.first
But it gives me this error:
Admin::ScholarshipGroup Load (0.3ms) SELECT "admin_scholarship_groups".* FROM "admin_scholarship_groups" ORDER BY "admin_scholarship_groups"."id" ASC LIMIT ? [["LIMIT", 1]]
ActiveRecord::InverseOfAssociationNotFoundError: Could not find the inverse association for admin_scholarships (:admin_scholarship_group in Admin::Scholarship)
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activerecord-5.0.0.1/lib/active_record/reflection.rb:202:in `check_validity_of_inverse!'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activerecord-5.0.0.1/lib/active_record/reflection.rb:402:in `check_validity!'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activerecord-5.0.0.1/lib/active_record/associations/association.rb:25:in `initialize'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activerecord-5.0.0.1/lib/active_record/associations.rb:235:in `new'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activerecord-5.0.0.1/lib/active_record/associations.rb:235:in `association'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activerecord-5.0.0.1/lib/active_record/associations/builder/association.rb:111:in `admin_scholarships'
from (irb):34
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/ubuntu/workspace/bin/rails:9:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1#college/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
Why do return this type error ? Can anyone suggest me the solution of this error ?
You defined belongs_to :group in Admin::Scholarship model.
You can try:
has_many :admin_scholarships, :class_name => 'Admin::Scholarship',inverse_of: :group, foreign_key: 'group_id'

How can I call a DB2 Stored Procedure with OUT params using ActiveRecord?

I need to call some legacy stored procedures using ActiveRecord.
I can call procedures using ActiveRecords when they have no parameters or only IN params.
e.g.
ActiveRecord::Base.connection.execute('call legacy_proc')
or
ActiveRecord::Base.connection.execute("call legacy_proc('param1', 'param2')")
The above works perfectly fine. Now, the problem arises when I need to call a procedure with OUT params. I tried different ways of calling the SP but I can't get it to work.
ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', ?, ?)")
ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', null, null)")
ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', '', '')")
None of the above work (the last two params are OUT params). As a last option, I could even skip reading the OUT params if that's really not possible, as long as I can at least execute the procedure.
EDIT: adding the error details in order to clarify "not working".
ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: com.ibm.db2.jcc.am.SqlException: DB2 SQL Error: SQLCODE=-313, SQLSTATE=07004, SQLERRMC=null, DRIVER=4.15.113: call legacy_proc_with_out_params('param1', 'param2', ?, ?)
from arjdbc/jdbc/RubyJdbcConnection.java:547:in `execute'
from /home/devusr.gem/gems/activerecord-jdbc-adapter-1.3.16/lib/arjdbc/jdbc/adapter.rb:581:in `_execute'
from /home/devusr.gem/gems/activerecord-jdbc-adapter-1.3.16/lib/arjdbc/jdbc/adapter.rb:557:in `execute'
from /home/devusr.gem/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `log'
from /home/devusr.gem/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/devusr.gem/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
from /home/devusr.gem/gems/activerecord-jdbc-adapter-1.3.16/lib/arjdbc/jdbc/adapter.rb:557:in `execute'
from (irb):30:in `evaluate'
from org/jruby/RubyKernel.java:1119:in `eval'
from org/jruby/RubyKernel.java:1519:in `loop'
from org/jruby/RubyKernel.java:1282:in `catch'
from org/jruby/RubyKernel.java:1282:in `catch'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in `start'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands.rb:17:in `(root)'
from org/jruby/RubyKernel.java:1083:in `require'
EDIT 2: tried #mustaccio's suggestion (i.e. use the prepare method through raw_connection, but the DB2 adapter does not implement that method. If there's an alternative path to an implementation of a similar method in DB2, this would definitely resolve the issue.
I also forgot to mention that I'm using jRuby. Therefore, I'm using the jdbc adapter for DB2.
irb(main):049:0> ActiveRecord::ConnectionAdapters.constants.grep /DB2/
=> [:DB2Column, :DB2JdbcConnection]
irb(main):050:0> ActiveRecord::Base.connection.raw_connection.methods.sort.grep /prep/
=> []
irb(main):051:0> ActiveRecord::Base.connection.raw_connection.prepare
NoMethodError: undefined method `prepare' for #<ActiveRecord::ConnectionAdapters::DB2JdbcConnection:0xe6bc1cd0>
from (irb):51:in `evaluate'
from org/jruby/RubyKernel.java:1119:in `eval'
from org/jruby/RubyKernel.java:1519:in `loop'
from org/jruby/RubyKernel.java:1282:in `catch'
from org/jruby/RubyKernel.java:1282:in `catch'
from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in `start'
from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'
from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands.rb:17:in `(root)'
from org/jruby/RubyKernel.java:1083:in `require'
from bin/rails:4:in `(root)'
The solution I found is to use a BEGIN-END block and declare dummy variables to bind to the OUT params.
Given this example:
ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', ?, ?)")
Where the first OUT param is INTEGER and the second is a SMALLINT, the complete call looks like this:
beginend_stmt = "BEGIN
DECLARE out_Param1 INTEGER;
DECLARE out_Param2 SMALLINT;
call legacy_proc_with_out_params('param1', 'param2', out_Param1, out_Param2);
END"
ActiveRecord::Base.connection.execute beginend_stmt

In 'initialize': wrong number of arguments (1 for 2..3) (ArgumentError)

So I have the class 'Range' created by Datamapper (this is in a Ruby/Sinatra project):
class Range
include DataMapper::Resource
#properties
property :id, Serial, :key => true
property :default_gateway, String
property :subnetmask, String
property :main_ip, String
property :backup_ip, String
property :dhcp, String
#relations
belongs_to :vlan, :required => true
has n, :dhcp
end #class Range
I also have a file 'initdata' which I call in my main class. A part of this file tries to fill up the database with some dummy data. The problem is with the class Ranges it gives me an ArgumentError.
Initdata ruby file:
#CREATE RANGES
if Range.count == 0
puts '----- ADDING RANGES -----'
range1 = Range.first_or_create(:default_gateway => '10.1.2.44', :subnetmask => '0.0.255.255', :main_ip => '10.1.0.1', :backup_ip => '10.1.0.2', :dhcp => '192.168.1.2', :vlan_id => 1)
range1.errors.each do |error|
puts error
end
end
I get the following error when I run it in the terminal:
----- ADDING RANGES -----
/home/bene/.rvm/gems/ruby-2.1.3/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `initialize': wrong number of arguments (1 for 2..3) (ArgumentError)
from /home/bene/.rvm/gems/ruby-2.1.3/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `new'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `create'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/dm-core-1.2.0/lib/dm-core/model.rb:444:in `first_or_create'
from /home/bene/project/development_sam/lan_portal/dal/initdata.rb:328:in `<top (required)>'
from /home/bene/project/development_sam/lan_portal/app.rb:6:in `require'
from /home/bene/project/development_sam/lan_portal/app.rb:6:in `<top (required)>'
from /home/bene/project/development_sam/lan_portal/config.ru:1:in `require'
from /home/bene/project/development_sam/lan_portal/config.ru:1:in `block in <main>'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from /home/bene/project/development_sam/lan_portal/config.ru:in `new'
from /home/bene/project/development_sam/lan_portal/config.ru:in `<main>'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:250:in `start'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
from /home/bene/.rvm/gems/ruby-2.1.3/gems/rack-1.5.2/bin/rackup:4:in `<top (required)>'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/rackup:23:in `load'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/rackup:23:in `<main>'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `eval'
from /home/bene/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `<main>'
Does anyone have any idea what I might be doing wrong? I've searched for it, but most solutions are because people wrote their own 'initialize' function, as in my case, it's already defined by DataMapper
There is a core Ruby class Range (which, incidentally, has a 2..3-argument constructor); Appearently, DataMapper does not redefine a constructor if one already exists.
You need to rename your class.

Resources