Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Papertrail 6 fails to find the version primary key #885

Open
CloCkWeRX opened this issue Aug 10, 2020 · 2 comments
Open

Papertrail 6 fails to find the version primary key #885

CloCkWeRX opened this issue Aug 10, 2020 · 2 comments

Comments

@CloCkWeRX
Copy link
Member

CloCkWeRX commented Aug 10, 2020

In some situations, doing:

contact = Contact.find(1)
contact.versions

results in invalid SQL being generated:

SELECT "versions".* FROM "versions" WHERE "versions"."item_id" = $1 AND "versions"."item_type" = $2 ORDER BY "versions"."created_at" ASC, "versions"."" ASC

The contact class has

 has_paper_trail class_name: 'Version', ignore: [:subscribed_users]

Which points to

        if ::ActiveRecord::VERSION::MAJOR >= 4
          has_many self.versions_association_name,
            lambda { order(model.timestamp_sort_order) },
            :class_name => self.version_class_name, :as => :item
        else
          has_many self.versions_association_name,
            :class_name => self.version_class_name,
            :as         => :item,
            :order      => self.paper_trail_version_class.timestamp_sort_order
        end

which does:

      def timestamp_sort_order(direction = "asc")
        [arel_table[:created_at].send(direction.downcase)].tap do |array|
          array << arel_table[primary_key].send(direction.downcase) if primary_key_is_int?
        end
      end

Here, the Version.primary_key is nil; even though it is specified in the db schema:

  create_table "versions", id: :serial, force: :cascade do |t|
    t.string "item_type", null: false
    t.integer "item_id", null: false
    t.string "event", limit: 512, null: false
    t.string "whodunnit"
    t.text "object"
    t.datetime "created_at"
    t.text "object_changes"
    t.integer "related_id"
    t.string "related_type"
    t.integer "transaction_id"
    t.index ["created_at"], name: "index_versions_on_created_at"
    t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
    t.index ["related_id", "related_type"], name: "index_versions_on_related_id_and_related_type"
    t.index ["transaction_id"], name: "index_versions_on_transaction_id"
    t.index ["whodunnit"], name: "index_versions_on_whodunnit"
  end

Database: Postgres 9.6?

    activerecord (5.1.6.2)
      activemodel (= 5.1.6.2)
      activesupport (= 5.1.6.2)
      arel (~> 8.0)

and

    pg (0.21.0)
@CloCkWeRX
Copy link
Member Author

Unique things - we do a few class_evals to extend Contact, Account classes in a funky order; but that shouldn't affect the Version table not being recognized

@CloCkWeRX
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant