mirror of
https://github.com/django/django.git
synced 2025-04-27 18:54:41 +00:00
When all values of a field with a db_default are DatabaseDefault, which is the case most of the time, there is no point in specifying explicit DEFAULT for all INSERT VALUES as that's what the database will do anyway if not specified. In the case of PostgreSQL doing so can even be harmful as it prevents the usage of the UNNEST strategy and in the case of Oracle, which doesn't support the usage of the DEFAULT keyword, it unnecessarily requires providing literal db defaults. Thanks Lily Foote for the review.
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
import unittest
|
|
|
|
from django.db import connection
|
|
from django.db.models.expressions import RawSQL
|
|
from django.test import TestCase
|
|
|
|
from ..models import Square
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL tests")
|
|
class BulkCreateUnnestTests(TestCase):
|
|
def test_single_object(self):
|
|
with self.assertNumQueries(1) as ctx:
|
|
Square.objects.bulk_create([Square(root=2, square=4)])
|
|
self.assertNotIn("UNNEST", ctx[0]["sql"])
|
|
|
|
def test_non_literal(self):
|
|
with self.assertNumQueries(1) as ctx:
|
|
Square.objects.bulk_create(
|
|
[Square(root=2, square=RawSQL("%s", (4,))), Square(root=3, square=9)]
|
|
)
|
|
self.assertNotIn("UNNEST", ctx[0]["sql"])
|
|
|
|
def test_unnest_eligible(self):
|
|
with self.assertNumQueries(1) as ctx:
|
|
Square.objects.bulk_create(
|
|
[Square(root=2, square=4), Square(root=3, square=9)]
|
|
)
|
|
self.assertIn("UNNEST", ctx[0]["sql"])
|
|
|
|
def test_unnest_eligible_db_default(self):
|
|
with self.assertNumQueries(1) as ctx:
|
|
squares = Square.objects.bulk_create([Square(root=3), Square(root=3)])
|
|
self.assertIn("UNNEST", ctx[0]["sql"])
|
|
self.assertEqual([square.square for square in squares], [9, 9])
|