jashmenn / activeuuid

Binary uuid keys in Rails
MIT License
340 stars 124 forks source link

Error with PostgreSQL #60

Closed dtelaroli closed 9 years ago

dtelaroli commented 9 years ago

Rails 4.2 Ruby 2.2.0

The error happens when I execute Model.create. With SQLite works fine.

TypeError: can't cast UUIDTools::UUID to uuid
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/quoting.rb:34:in `rescue in type_cast'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/quoting.rb:23:in `type_cast'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activeuuid-0.6.0/lib/activeuuid/patches.rb:128:in `type_cast_with_visiting'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_cache'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:597:in `map'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:597:in `exec_cache'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:192:in `exec_insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:108:in `insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:64:in `insert'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:521:in `_create_record'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/counter_cache.rb:139:in `_create_record'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:127:in `_create_record'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/callbacks.rb:306:in `block in _create_record'
... 20 levels...
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:286:in `block in save'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:285:in `save'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:34:in `create'
    from (irb):4
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /opt/code/lista_compras_server/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'2.2.0 :005
dtelaroli commented 9 years ago

Any suggestions?

dtelaroli commented 9 years ago

I created the this patch and work (returns the same value as string).

module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module OID # :nodoc:
        class Uuid < Type::Value # :nodoc:
          def type_cast_from_user(value)
            #UUIDTools::UUID.serialize(value) if value
            value
          end
          alias_method :type_cast_from_database, :type_cast_from_user
        end
      end
    end
  end
end
mvoloz commented 9 years ago

Any updates on this? I'm having same issue.

imgarylai commented 9 years ago

I have same problem.

[2] pry(main)> o.save
   (0.1ms)  BEGIN
   (0.2ms)  ROLLBACK
TypeError: can't cast UUIDTools::UUID to uuid
from /Users/gary/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/quoting.rb:34:in `rescue in type_cast'
nathanielks commented 9 years ago

+1

mchadwick commented 9 years ago

+1

jairud commented 9 years ago

+1 to give the same UUID support in Mysql, Postgres and sqlite

imgarylai commented 9 years ago

Since Rails 4 with PostgreSQL, we can use uuid as our primary key.

Here is my solution, but I only use it on Rails 4.2.1 with PostgreSQL 9.3.

First, create a migration. Be sure this migration before any migrations you want to implement with uuid.

rails g migration enable_uuid_extension

And inside the migration file:

class EnableUuidOsspExtension < ActiveRecord::Migration
  def change
    enable_extension 'uuid-ossp'
  end
end

Then Here is an example Model with uuid:

class CreateOrders < ActiveRecord::Migration
  def change
    create_table :orders, id: :uuid do |t|
      t.uuid :user_id, index: true, foreign_key: true
      t.string :full_name
      t.string :email
      t.string :country
      t.string :county
      t.string :zip_code
      t.string :district
      t.string :street_address
      t.string :phone
      t.decimal :tax, precision: 12, scale: 3
      t.decimal :shipping, precision: 12, scale: 3
      t.decimal :subtotal, precision: 12, scale: 3
      t.decimal :total, precision: 12, scale: 3
      t.text :notification_params
      t.string :workflow_state
      t.string :status
      t.string :transaction_id
      t.datetime :purchased_at

      t.timestamps null: false
    end
  end
end
dtelaroli commented 9 years ago

It's works fine. Thank you.

frenkel commented 8 years ago

Neither the extension nor monkey-patching fixes this for me. I'm using Ruby on Rails 4.2.4, ActiveUuid 0.6.1 and PostgreSQL 9.1. What would be the correct way to fix this?

frenkel commented 8 years ago

Ah, for me it is a different function. I'll create a pull-request!

iroth commented 8 years ago

having the same issue, running the migration with create extension requires super user and if I run this on the database from psql it did not help. I also have the latest with above mentioned patch and still i get the error. not sure what is wrong

jetienne commented 8 years ago

You don't need this gem while using PostgreSQL. Remove it from Gemfile, remove include on your model, and change a bit the migration definition like explained here: http://blog.arkency.com/2014/10/how-to-start-using-uuid-in-activerecord-with-postgresql/

wangthony commented 7 years ago

If you want to set a UUID attribute value, you will still trigger the error. Try putting this in an initializer:

module PostgresqlUuidQuotingPatch
  extend ActiveSupport::Concern

  included do
    def _type_cast_with_quoting(value)
      if value.is_a?(UUIDTools::UUID)
        value.to_s
      else
        _type_cast_without_quoting(value)
      end
    end

    alias_method_chain :_type_cast, :quoting
  end
end

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :include, PostgresqlUuidQuotingPatch

(Only tested on ActiveRecord 4.2)

abdalaklinch commented 7 years ago

wangthony, this worked for me as far as getting passed this error. However, I now get: " type modifier is not allowed for type "uuid"", would this be related to this Issue?

wangthony commented 7 years ago

not sure… what does the stack trace look like?

On Nov 14, 2016, at 10:17 AM, abdalaklinch notifications@github.com wrote:

wangthony, this worked for me as far as getting passed this error. However, I now get: " type modifier is not allowed for type "uuid"", would this be related to this Issue?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jashmenn/activeuuid/issues/60#issuecomment-260415634, or mute the thread https://github.com/notifications/unsubscribe-auth/ABP-kw0Ccvf-N0kYsOYAiA41CkRseuNxks5q-KWkgaJpZM4DsrBe.