Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

friendly_id Error when migrating with history plugin #299

Closed
malomalo opened this Issue Jun 12, 2012 · 14 comments

Comments

Projects
None yet
3 participants

I get the following when Trying to call any model or reseting the column information on a model:

==  AddCounterCachesToAddresses: migrating ====================================
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
: SELECT  "addresses".* FROM "addresses"  LIMIT 1
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:1151:in `async_exec'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:1151:in `exec_no_cache'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `block in exec_query'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in `exec_query'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:1246:in `select'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/querying.rb:38:in `block in find_by_sql'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/explain.rb:40:in `logging_query_plan'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/querying.rb:37:in `find_by_sql'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/relation.rb:171:in `exec_queries'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/relation.rb:160:in `block in to_a'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/explain.rb:40:in `logging_query_plan'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/relation.rb:159:in `to_a'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/relation/finder_methods.rb:376:in `find_first'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/relation/finder_methods.rb:122:in `first'
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_record/querying.rb:5:in `first'
/Users/jon/code/mls/db/migrate/20120611220614_add_counter_caches_to_addresses.rb:3:in `change'

I've narrowed it down to when the history plugin is used. The logs show the following query that errors in Postgres:

PG::Error: ERROR:  relation "slugs" does not exist
LINE 4:              WHERE a.attrelid = '"slugs"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"slugs"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

  Address Load (1.1ms)  SELECT "addresses".* FROM "addresses" LIMIT 1
PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
: SELECT  "addresses".* FROM "addresses"  LIMIT 1
   (0.1ms)  ROLLBACK
Connecting to database specified by database.yml

Been trying all day to get to the bottom of this, but no success yet.

Collaborator

parndt commented Jun 12, 2012

Looks like the slugs table doesn't exist?

@parndt you are correct, but shouldn't it be using the friendly_id_slugs table? I'm using gem version 4.0.7

schema.rb:

create_table "friendly_id_slugs", :force => true do |t|
    t.string   "slug",                         :null => false
    t.integer  "sluggable_id",                 :null => false
    t.string   "sluggable_type", :limit => 40
    t.datetime "created_at"
end

add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true
add_index "friendly_id_slugs", ["sluggable_id"], :name => "index_friendly_id_slugs_on_sluggable_id"
add_index "friendly_id_slugs", ["sluggable_type"], :name => "index_friendly_id_slugs_on_sluggable_type"
Collaborator

parndt commented Jun 12, 2012

Yes, according to this documentation it should be friendly_id_slugs https://github.com/norman/friendly_id/blob/master/lib/friendly_id/history.rb

Did you follow those instructions exactly?

https://github.com/norman/friendly_id/blob/master/lib/friendly_id/slug.rb This indicates the table_name is being set correctly..

Can you go into the console and run:

FriendlyId::Slug.table_name

and report the result please?

Not sure why it won't find it when I first bring it up and why I need to attempt to access another model first..

Loading development environment (Rails 3.2.5)
irb(main):001:0> FriendlyId::Slug.table_name
NameError: uninitialized constant FriendlyId::Slug
    from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing_from_s3_library'
    from (irb):1
    from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):002:0> Address.first
  Address Load (1.1ms)  SELECT "addresses".* FROM "addresses" LIMIT 1
=> #<Address id: 473...>
irb(main):003:0> FriendlyId::Slug.table_name
=> "friendly_id_slugs"
Collaborator

parndt commented Jun 12, 2012

I'm guessing that the FriendlyId library isn't loaded until that point. :-)

OK so you're getting the right table_name which makes this stranger because the history code just asks Slug all the details: https://github.com/norman/friendly_id/blob/master/lib/friendly_id/history.rb#L66-L67

In the migration if I try just outputting FriendlyId::Slug.table_name and I get the following:

uninitialized constant FriendlyId::Slug

not sure if that is helpful or not

Collaborator

parndt commented Jun 12, 2012

what if, in the migration, you call:

require 'friendly_id'

require 'friendly_id' still results in uninitialized constant FriendlyId::Slug

require 'friendly_id/slug' however causes the following:

An error has occurred, this and all later migrations canceled:

PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

with this in the log

PG::Error: ERROR:  relation "slugs" does not exist
LINE 4:              WHERE a.attrelid = '"slugs"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"slugs"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

friendly_id_slugs
PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

PG::Error: ERROR:  current transaction is aborted, commands ignored until end of transaction block
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum
Collaborator

parndt commented Jun 12, 2012

Strange. I'm out of ideas and will have to escalate to @norman :-)

Not sure whats going on but when history is included it appears that the FriendlyId::Slug is not being loaded. Including require 'friendly_id/slug' in application.rb fixes the issues.

Perhaps requiring it in the migration is too late?

Collaborator

parndt commented Jun 12, 2012

Weeeeird

norman added a commit that referenced this issue Jun 19, 2012

Autoload slug rather than requiring directly
Without this change, FriendlyId::Slug is not accessible from migrations
unless friendly_id/slug is explicitly required.

This should resolve issue #299.
Owner

norman commented Jun 19, 2012

@malomalo Thanks for the detailed report and followup, and sorry for the slow response. I was able to reproduce your problem locally, and I've just now pushed a change which should fix it. Can you let me know if it works for you?

@norman sorry for the late reply, I'll try and check it out this weekend

Owner

norman commented Apr 16, 2013

Closing this since it was fixed and already released a while ago.

@norman norman closed this Apr 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment