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

Avoid double unique check in rails and friendly_id #967

Open
plewasebastian opened this issue Feb 16, 2021 · 3 comments
Open

Avoid double unique check in rails and friendly_id #967

plewasebastian opened this issue Feb 16, 2021 · 3 comments
Labels

Comments

@plewasebastian
Copy link

I use friendly_id to generate slug i my rails 6 application.
I realized that the check for a unique name is done twice for each record addition.

    TRANSACTION (0.1ms)  BEGIN
    Car Exists? (0.1ms)  SELECT 1 AS one FROM "cars" WHERE "carss"."id" IS NOT NULL AND "cars"."slug" = $1 LIMIT $2  [["slug", "audi-4"], ["LIMIT", 1]]
    Car Exists? (0.2ms)  SELECT 1 AS one FROM "cars" WHERE "carss"."id" IS NOT NULL AND "cars"."slug" = $1 LIMIT $2  [["slug", "audi-4"], ["LIMIT", 1]]
    Car Create (0.2ms)  INSERT INTO "cars" ("name", "p", "slug", "created_at", "updated_at", "x") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "Audi-4"], ["p", "1500099948CC"], ["slug", "audi-4"], ["created_at", "2021-02-16 13:22:16.298773"], ["updated_at", "2021-02-16 13:22:16.298773"], ["x", "hhc3av7GPbRzBsYwRPFiPIkQhRA4R8G9"]]
    TRANSACTION (0.8ms)  COMMIT

If I delete:

      def should_generate_new_friendly_id?
        name_changed?
      end

it only executes once.

It looks like the check is also done on add, not as it would seem on update.

How do I prevent the name from being checked for uniqueness twice?

@pacMakaveli
Copy link

pacMakaveli commented Mar 28, 2021

Bump! I'm seeing the same issue in my app. Noticed when my queries count skyrocketed.
I personally don't use the should_generate.. method, so I don't think it's that.

PlayStation::Identity Load (0.7ms)  SELECT `play_station_identities`.* FROM `play_station_identities` ORDER BY `play_station_identities`.`id` ASC LIMIT 1
  PlayStation::Identity Load (3.1ms)  SELECT `play_station_identities`.* FROM `play_station_identities` WHERE `play_station_identities`.`slug` = '1' LIMIT 1
  PlayStation::Identity Load (5.4ms)  SELECT `play_station_identities`.* FROM `play_station_identities` WHERE `play_station_identities`.`slug` = '1' LIMIT 1
  PlayStation::Identity Load (2.6ms)  SELECT `play_station_identities`.* FROM `play_station_identities` WHERE `play_station_identities`.`id` = 1 LIMIT 1

Is there a way to remove the additional query?

@stale
Copy link

stale bot commented Jun 20, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 20, 2021
@parndt parndt added the pinned label Jun 21, 2021
@stale stale bot removed the stale label Jun 21, 2021
@davidalejandroaguilar
Copy link

Same here, I'm getting 4 of them when it looks like it should only be 2 or probably only 1:

SELECT 1 AS one FROM "deals" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_type" = $1 AND "friendly_id_slugs"."sluggable_id" = "deals"."id" WHERE "deals"."id" IS NOT NULL AND "deals"."slug" = $2 LIMIT $3
SELECT 1 AS one FROM "deals" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_type" = $1 AND "friendly_id_slugs"."sluggable_id" = "deals"."id" WHERE "deals"."id" IS NOT NULL AND "friendly_id_slugs"."sluggable_type" = 'Deal' AND "friendly_id_slugs"."slug" = 'rustic-bronze-computer' LIMIT $2
SELECT 1 AS one FROM "deals" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_type" = $1 AND "friendly_id_slugs"."sluggable_id" = "deals"."id" WHERE "deals"."id" IS NOT NULL AND "deals"."slug" = $2 LIMIT $3
SELECT 1 AS one FROM "deals" INNER JOIN "friendly_id_slugs" ON "friendly_id_slugs"."sluggable_type" = $1 AND "friendly_id_slugs"."sluggable_id" = "deals"."id" WHERE "deals"."id" IS NOT NULL AND "friendly_id_slugs"."sluggable_type" = 'Deal' AND "friendly_id_slugs"."slug" = 'rustic-bronze-computer' LIMIT $2

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

No branches or pull requests

4 participants