Skip to content

Fix DurationField, IntegerField unique constraints for values < or > 32 bits #358

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

timgraham
Copy link
Collaborator

@timgraham timgraham commented Aug 6, 2025

Whether values less than or greater than 32 bits were problematic depends on the field.

@timgraham timgraham force-pushed the integerfield-db-type branch from ef9ebdf to 5f881bd Compare August 6, 2025 20:58
@timgraham timgraham changed the title Fix unique constraints for values > 32 bits on various IntegerFields. Fix IntegerField unique constraints for values < or > 32 bits, depending on the field Aug 6, 2025
@timgraham timgraham changed the title Fix IntegerField unique constraints for values < or > 32 bits, depending on the field Fix unique constraints for values < 32 bits on some IntegerFields and > 32 bits on others Aug 6, 2025
@timgraham timgraham force-pushed the integerfield-db-type branch 3 times, most recently from e201cbe to 59904f4 Compare August 7, 2025 17:48
@timgraham timgraham changed the title Fix unique constraints for values < 32 bits on some IntegerFields and > 32 bits on others Fix DurationField, IntegerField unique constraints for values < or > 32 bits Aug 7, 2025
@@ -33,7 +33,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: 'mongodb-forks/django'
ref: 'mongodb-5.2.x'
ref: 'ints-as-long'
Copy link
Collaborator Author

@timgraham timgraham Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch contains backward incompatible changes to the test suite and will be merged to mongodb-5.2.x before this patch:

diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py
index da9d190096..83aa4c531a 100644
--- a/tests/expressions/tests.py
+++ b/tests/expressions/tests.py
@@ -1760,10 +1760,8 @@ class ExpressionOperatorTests(TestCase):
         Number.objects.filter(pk=self.n.pk).update(
             integer=640 / F("integer"), float=42.7 / F("float")
         )
-        # Unlike SQL, MongoDB doesn't truncate decimals for integer division.
-        self.assertEqual(
-            Number.objects.get(pk=self.n.pk).integer, Approximate(15.238, places=3)
-        )
+
+        self.assertEqual(Number.objects.get(pk=self.n.pk).integer, 15)
         self.assertEqual(
             Number.objects.get(pk=self.n.pk).float, Approximate(2.755, places=3)
         )
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 3e226cd6a3..2c3e26cfc6 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -553,9 +553,11 @@ class OperationTests(OperationTestBase):
         self.assertTableExists("test_crmo_pony")
         # Test constraint works
         Pony = new_state.apps.get_model("test_crmo", "Pony")
-        Pony.objects.create(pink=1, weight=4.0)
-        Pony.objects.create(pink=1, weight=4.0)
-        Pony.objects.create(pink=1, weight=6.0)
+        from bson import Int64
+
+        Pony.objects.create(pink=Int64(1), weight=4.0)
+        Pony.objects.create(pink=Int64(1), weight=4.0)
+        Pony.objects.create(pink=Int64(1), weight=6.0)
         if connection.features.supports_partial_indexes:
             with self.assertRaises(IntegrityError):
                 Pony.objects.create(pink=1, weight=7.0)
@@ -3640,16 +3642,18 @@ class OperationTests(OperationTestBase):
         # cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
         # cursor.execute("DELETE FROM test_alunto_pony")
         pony = connection.database["test_alunto_pony"]
-        pony.insert_one({"pink": 1, "weight": 1.0})
-        pony.insert_one({"pink": 1, "weight": 1.0})
+        from bson import Int64
+
+        pony.insert_one({"pink": Int64(1), "weight": 1.0})
+        pony.insert_one({"pink": Int64(1), "weight": 1.0})
         pony.delete_many({})
         # Test the database alteration
         with connection.schema_editor() as editor:
             operation.database_forwards("test_alunto", editor, project_state, new_state)
         # cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
-        pony.insert_one({"pink": 1, "weight": 1.0})
+        pony.insert_one({"pink": Int64(1), "weight": 1.0})
         with self.assertRaises(DuplicateKeyError):
-            pony.insert_one({"pink": 1, "weight": 1.0})
+            pony.insert_one({"pink": Int64(1), "weight": 1.0})
             # with atomic():
             #     cursor.execute(
             #         "INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)"
@@ -3664,8 +3668,8 @@ class OperationTests(OperationTestBase):
         # cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
         # cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
         # cursor.execute("DELETE FROM test_alunto_pony")
-        pony.insert_one({"pink": 1, "weight": 1.0})
-        pony.insert_one({"pink": 1, "weight": 1.0})
+        pony.insert_one({"pink": Int64(1), "weight": 1.0})
+        pony.insert_one({"pink": Int64(1), "weight": 1.0})
         pony.delete_many({})
         # Test flat unique_together
         operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
I didn't update the evergreen config to use this temporary branch which is why those tests are failing.

@timgraham timgraham requested review from Jibola and WaVEV August 7, 2025 18:22
@timgraham timgraham force-pushed the integerfield-db-type branch 4 times, most recently from f810e16 to a92073f Compare August 11, 2025 01:16
@timgraham timgraham force-pushed the integerfield-db-type branch from a92073f to 6a24003 Compare August 11, 2025 02:08
@timgraham timgraham requested a review from Jibola August 11, 2025 09:48
…32 bits

Whether values less than or greater than 32 bits were problematic
depends on the field.
@timgraham timgraham force-pushed the integerfield-db-type branch from 6a24003 to 1002678 Compare August 11, 2025 14:28
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

Successfully merging this pull request may close these issues.

2 participants