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

generates 2 validations for a multi-column unique constraint #30

Open
urkle opened this issue Nov 30, 2015 · 2 comments
Open

generates 2 validations for a multi-column unique constraint #30

urkle opened this issue Nov 30, 2015 · 2 comments

Comments

@urkle
Copy link
Member

urkle commented Nov 30, 2015

I have this schema

  create_table "authorizations" do |t|
    t.integer  "user_id",               null: false, index: {name: "index_authorizations_on_user_id"}, foreign_key: {references: "users", name: "fk_authorizations_user_id", on_update: :restrict, on_delete: :cascade}
    t.integer  "client_application_id", null: false, index: {name: "index_authorizations_on_client_application_id_and_nonce", with: ["nonce"], unique: true}, foreign_key: {references: "client_applications", name: "fk_authorizations_client_application_id", on_update: :restrict, on_delete: :cascade}
    t.string   "code",                  null: false, index: {name: "index_authorizations_on_code", unique: true}
    t.string   "nonce",                 null: false
end

Here is the list of validations generated

[schema_validations] Authorization.validates_presence_of :code
[schema_validations] Authorization.validates_uniqueness_of :code, :allow_nil=>true, :if=>#<Proc:0x000000063a24c8@/home/urkle/.rvm/gems/ruby-2.1.5@rails4/gems/schema_validations-1.4.0/lib/schema_validations/active_record/validations.rb:167>
[schema_validations] Authorization.validates_presence_of :nonce
[schema_validations] Authorization.validates_uniqueness_of :nonce, :scope=>[:client_application_id], :allow_nil=>true, :if=>#<Proc:0x00000006387bc8@/home/urkle/.rvm/gems/ruby-2.1.5@rails4/gems/schema_validations-1.4.0/lib/schema_validations/active_record/validations.rb:167>
[schema_validations] Authorization.validates_presence_of :user
[schema_validations] Authorization.validates_presence_of :client_application
[schema_validations] Authorization.validates_uniqueness_of :client_application_id, :scope=>[:nonce], :allow_nil=>true, :if=>#<Proc:0x0000000637cac0@/home/urkle/.rvm/gems/ruby-2.1.5@rails4/gems/schema_validations-1.4.0/lib/schema_validations/active_record/validations.rb:167>

Notice the two validations created for the multi-column constraint. One for client_app_id, nonce.. And one for nonce, client_app_id

Also it seems odd that a validation with "allow_nil: true" would be generated when clearly I do not allow nils.

@urkle
Copy link
Member Author

urkle commented Nov 30, 2015

BTW.. I'm using schema_validations 1.4.0

And this ends up causing ActiveRecord to issue 2 DB checks

  Authorization Exists (1.2ms)  SELECT  1 AS one FROM "authorizations" WHERE ("authorizations"."nonce" = 'test123' AND "authorizations"."client_application_id" = 1) LIMIT 1
  Authorization Exists (1.1ms)  SELECT  1 AS one FROM "authorizations" WHERE ("authorizations"."client_application_id" = 1 AND "authorizations"."nonce" = 'test123') LIMIT 1

Also, the "code" validations causes 2 queries to also be issued.

  Authorization Exists (0.8ms)  SELECT  1 AS one FROM "authorizations" WHERE "authorizations"."code" = $1 LIMIT 1  [["code", "040cd859525d7dcd42b4ab99fa7b23ae0d9ec0bc472a730874f2d810bd4ef0ec"]]
  Authorization Exists (1.0ms)  SELECT  1 AS one FROM "authorizations" WHERE "authorizations"."code" = '040cd859525d7dcd42b4ab99fa7b23ae0d9ec0bc472a730874f2d810bd4ef0ec' LIMIT 1

@ronen
Copy link
Member

ronen commented Jan 19, 2016

@urkle thanks for the report and sorry for the slow reply.

You're right, schema_validations ought to be able to optimize away those duplicate validations. Though I must admit I'm not certain whether there's complete symmetry between these two:

validates :a, uniqueness: { scope: :b }
validates :b, uniqueness: { scope: :a }

And if they're not symmetric, how should schema_validations choose which to issue?

(BTW Using modern syntax above, see #33)

And agreed it's odd to have allow_nil when nil isn't allowed.

Unfortunately no time for me to dive into these right now--especially since there's a workaround: disable schema_validations for the offending column(s) and manually validate them appropriately--but PRs happily accepted!

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

2 participants