Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

get_primary_key logic complicates using updateable views (mysql) #10247

Closed
marcin-rzeznicki opened this Issue · 7 comments

6 participants

@marcin-rzeznicki

I suspect that this case may be a little rare, but still. When you create model backed by update-able view and make it subclass of abstract class you end up with UnknownPrimaryKey. Please consider:

class PersistentObject < ActiveRecord::Base
  self.abstract_class = true
end

class  Anything < PersistentObject
end

Now, how get_primary_key works in this case is that, since Base != self, it tries to get PK info from connection's schema cache.

def get_primary_key(base_name) #:nodoc:
          return 'id' if base_name.blank?

          case primary_key_prefix_type
          when :table_name
            base_name.foreign_key(false)
          when :table_name_with_underscore
            base_name.foreign_key
          else
            if ActiveRecord::Base != self && table_exists?
              connection.schema_cache.primary_keys(table_name)
            else
              'id'
            end
          end
        end

This fails for mysql (at least) because information about view retrieved by SHOW FULL FIELDS does not contain primary fields info. IMO it'd be better to just return id in this case. Thanks

@dmathieu
Collaborator

Hello,
Could you setup a simple repository with a rails app reproducing this issue ?

@marcin-rzeznicki

Hi, Sorry - this's been embarassing - I switched to other e-mail account and forgot to reset Github notifications. I'll try to recreate the issue with Rails 4.0.1 anytime soon.

@marcin-rzeznicki

I created a repo containing bare rails app showing this issue (https://github.com/marcin-rzeznicki/rails-issue-10247). It dawned on me that existence of abstract superclass was not really needed to reproduce this error, so I got rid of it. When you test the app you'll notice two failing tests in the app - one of them shows that primary key cannot be obtained from a view (see migrations to find out the details) in mysql, the other shows that even though view is updateable and AR can insert data through this view it cannot build correct find query (amusing, isn't it?)

@rafaelfranca
Owner

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@robin850
Collaborator

I'm able to reproduce the problem with 4-1-stable with the provided application ; the tests look good to me.

@robin850 robin850 removed the stale label
@rails-bot rails-bot added the stale label
@rails-bot
Collaborator

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@senny senny closed this issue from a commit
@senny senny models backed by views don't assume "id" columns are the primary key.
Closes #10247.

The same goes for tables with an "id" column but without primary key constraint.
Reading from the view works without configuration. If you have an updateable view
you need to use `self.primary_key = ''`.
fde0d02
@senny senny closed this in fde0d02
@trungpham trungpham referenced this issue from a commit in trungpham/rails
@senny senny models backed by views don't assume "id" columns are the primary key.
Closes #10247.

The same goes for tables with an "id" column but without primary key constraint.
Reading from the view works without configuration. If you have an updateable view
you need to use `self.primary_key = ''`.
0c298cb
@sachin004 sachin004 referenced this issue from a commit in sachin004/rails
@senny senny models backed by views don't assume "id" columns are the primary key.
Closes #10247.

The same goes for tables with an "id" column but without primary key constraint.
Reading from the view works without configuration. If you have an updateable view
you need to use `self.primary_key = ''`.
dab0f0e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.