Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5738

Overridden equality operator can result in behavioral difference in persistence between ActiveRecord and Mongoid

    • Type: Icon: Task Task
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Persistence
    • None
    • Ruby Drivers

      See https://github.com/carlwiedemann/mongoid_poc/commit/0e99e6df928ea57eeeec79cbe1ca8677c9339a0f for a demonstration.

      A similar example using AR/Sqlite can be found and modified at https://gist.github.com/alexbevi/8e3da5bb950c18eb21a089f13d78199d.

      Mongoid:

      #<Address _id: 65c2a8eb057cb6bf03f8b3c3, street_primary: "3902 Midsummer Ln S", street_secondary: nil, city: "Colorado Springs", province: "Colorado", postal_code: "80917", country: "US", person_id: BSON::ObjectId('65c2a8eb057cb6bf03f8b3c2')>
      #<Person _id: 65c2a8eb057cb6bf03f8b3c2, first_name: "Martin", last_name: "Fowler">
      #<Address _id: 65c2a8eb057cb6bf03f8b3c3, street_primary: "3902 Midsummer Ln S", street_secondary: nil, city: "Colorado Springs", province: "Colorado", postal_code: "80917", country: "US", person_id: BSON::ObjectId('65c2a8eb057cb6bf03f8b3c2')>
      #<Person _id: 65c2a8eb057cb6bf03f8b3c2, first_name: "Martin", last_name: "Fowler">
      

      AR

      -- create_table(:people, {:force=>true})
      D, [2024-02-06T13:48:17.655167 #49243] DEBUG -- :   [1m [35m (0.0ms) [0m   [1m [35mDROP TABLE IF EXISTS "people" [0m
      D, [2024-02-06T13:48:17.655338 #49243] DEBUG -- :   [1m [35m (0.1ms) [0m   [1m [35mCREATE TABLE "people" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "first_name" varchar, "last_name" varchar) [0m
         -> 0.0078s
      -- create_table(:addresses, {:force=>true})
      D, [2024-02-06T13:48:17.655461 #49243] DEBUG -- :   [1m [35m (0.0ms) [0m   [1m [35mDROP TABLE IF EXISTS "addresses" [0m
      D, [2024-02-06T13:48:17.655554 #49243] DEBUG -- :   [1m [35m (0.0ms) [0m   [1m [35mCREATE TABLE "addresses" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "street_primary" varchar, "street_secondary" varchar, "city" varchar, "province" varchar, "postal_code" varchar, "country" varchar, "person_id" integer) [0m
      D, [2024-02-06T13:48:17.655649 #49243] DEBUG -- :   [1m [35m (0.0ms) [0m   [1m [35mCREATE INDEX "index_addresses_on_person_id" ON "addresses" ("person_id") [0m
         -> 0.0003s
      D, [2024-02-06T13:48:17.656094 #49243] DEBUG -- :   [1m [35m (0.0ms) [0m   [1m [35mCREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY) [0m
      D, [2024-02-06T13:48:17.656597 #49243] DEBUG -- :   [1m [35m (0.0ms) [0m   [1m [35mCREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL) [0m
      D, [2024-02-06T13:48:17.667644 #49243] DEBUG -- :   [1m [36mActiveRecord::InternalMetadata Load (0.8ms) [0m   [1m [34mSELECT * FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? ORDER BY "ar_internal_metadata"."key" ASC LIMIT 1 [0m  [[nil, "environment"]]
      D, [2024-02-06T13:48:17.667797 #49243] DEBUG -- :   [1m [36mActiveRecord::InternalMetadata Create (0.0ms) [0m   [1m [32mINSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ('environment', 'default_env', '2024-02-06 21:48:17.667673', '2024-02-06 21:48:17.667674') RETURNING "key" [0m
      D, [2024-02-06T13:48:17.674758 #49243] DEBUG -- :   [1m [36mTRANSACTION (0.0ms) [0m   [1m [36mbegin transaction [0m
      D, [2024-02-06T13:48:17.674823 #49243] DEBUG -- :   [1m [36mPerson Create (0.1ms) [0m   [1m [32mINSERT INTO "people" ("first_name", "last_name") VALUES (?, ?) RETURNING "id" [0m  [["first_name", "Martin"], ["last_name", "Fowler"]]
      D, [2024-02-06T13:48:17.674899 #49243] DEBUG -- :   [1m [36mTRANSACTION (0.0ms) [0m   [1m [36mcommit transaction [0m
      D, [2024-02-06T13:48:17.682113 #49243] DEBUG -- :   [1m [36mAddress Load (0.0ms) [0m   [1m [34mSELECT "addresses".* FROM "addresses" WHERE "addresses"."person_id" = ? LIMIT ? [0m  [["person_id", 1], ["LIMIT", 1]]
      D, [2024-02-06T13:48:17.682761 #49243] DEBUG -- :   [1m [36mTRANSACTION (0.0ms) [0m   [1m [36mbegin transaction [0m
      D, [2024-02-06T13:48:17.682818 #49243] DEBUG -- :   [1m [36mAddress Create (0.1ms) [0m   [1m [32mINSERT INTO "addresses" ("street_primary", "city", "province", "postal_code", "country", "person_id") VALUES (?, ?, ?, ?, ?, ?) RETURNING "id" [0m  [["street_primary", "3902 Midsummer Ln S"], ["city", "Colorado Springs"], ["province", "Colorado"], ["postal_code", "80917"], ["country", "US"], ["person_id", 1]]
      D, [2024-02-06T13:48:17.682869 #49243] DEBUG -- :   [1m [36mTRANSACTION (0.0ms) [0m   [1m [36mcommit transaction [0m
      D, [2024-02-06T13:48:17.683117 #49243] DEBUG -- :   [1m [36mPerson Load (0.0ms) [0m   [1m [34mSELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT ? [0m  [["id", 1], ["LIMIT", 1]]
      D, [2024-02-06T13:48:17.683282 #49243] DEBUG -- :   [1m [36mAddress Load (0.0ms) [0m   [1m [34mSELECT "addresses".* FROM "addresses" WHERE "addresses"."person_id" = ? LIMIT ? [0m  [["person_id", 1], ["LIMIT", 1]]
      #<Address:0x000000013dc59840
       id: 1,
       street_primary: "3902 Midsummer Ln S",
       street_secondary: nil,
       city: "Colorado Springs",
       province: "Colorado",
       postal_code: "80917",
       country: "US",
       person_id: 1>
      D, [2024-02-06T13:48:17.685182 #49243] DEBUG -- :   [1m [36mPerson Load (0.0ms) [0m   [1m [34mSELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT ? [0m  [["id", 1], ["LIMIT", 1]]
      #<Person:0x000000013da526f0 id: 1, first_name: "Martin", last_name: "Fowler">
      D, [2024-02-06T13:48:17.685385 #49243] DEBUG -- :   [1m [36mAddress Load (0.0ms) [0m   [1m [34mSELECT "addresses".* FROM "addresses" WHERE "addresses"."person_id" = ? LIMIT ? [0m  [["person_id", 1], ["LIMIT", 1]]
      D, [2024-02-06T13:48:17.685696 #49243] DEBUG -- :   [1m [36mTRANSACTION (0.0ms) [0m   [1m [36mbegin transaction [0m
      D, [2024-02-06T13:48:17.685736 #49243] DEBUG -- :   [1m [36mAddress Update (0.1ms) [0m   [1m [33mUPDATE "addresses" SET "person_id" = ? WHERE "addresses"."id" = ? [0m  [["person_id", nil], ["id", 1]]
      D, [2024-02-06T13:48:17.685912 #49243] DEBUG -- :   [1m [36mAddress Create (0.0ms) [0m   [1m [32mINSERT INTO "addresses" ("street_primary", "city", "province", "postal_code", "country", "person_id") VALUES (?, ?, ?, ?, ?, ?) RETURNING "id" [0m  [["street_primary", "3902 Midsummer Ln S"], ["city", "Denver"], ["province", "Colorado"], ["postal_code", "80917"], ["country", "US"], ["person_id", 1]]
      D, [2024-02-06T13:48:17.685966 #49243] DEBUG -- :   [1m [36mTRANSACTION (0.0ms) [0m   [1m [36mcommit transaction [0m
      D, [2024-02-06T13:48:17.686091 #49243] DEBUG -- :   [1m [36mPerson Load (0.0ms) [0m   [1m [34mSELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT ? [0m  [["id", 1], ["LIMIT", 1]]
      D, [2024-02-06T13:48:17.686217 #49243] DEBUG -- :   [1m [36mAddress Load (0.0ms) [0m   [1m [34mSELECT "addresses".* FROM "addresses" WHERE "addresses"."person_id" = ? LIMIT ? [0m  [["person_id", 1], ["LIMIT", 1]]
      #<Address:0x000000013dcd3078
       id: 2,
       street_primary: "3902 Midsummer Ln S",
       street_secondary: nil,
       city: "Denver",
       province: "Colorado",
       postal_code: "80917",
       country: "US",
       person_id: 1>
      D, [2024-02-06T13:48:17.686414 #49243] DEBUG -- :   [1m [36mPerson Load (0.0ms) [0m   [1m [34mSELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT ? [0m  [["id", 1], ["LIMIT", 1]]
      #<Person:0x000000013da526f0 id: 1, first_name: "Martin", last_name: "Fowler">
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: