Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions django/core/management/commands/inspectdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def add_arguments(self, parser):
)

def handle(self, **options):
try:
if connections[options["database"]].features.supports_inspectdb:
for line in self.handle_inspection(options):
self.stdout.write(line)
except NotImplementedError:
else:
raise CommandError(
"Database inspection isn't supported for the currently selected "
"database backend."
Expand Down
3 changes: 3 additions & 0 deletions django/db/backends/base/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ class BaseDatabaseFeatures:
supports_on_delete_db_default = True
supports_on_delete_db_null = True

# Does the backend support the inspectdb management command?
supports_inspectdb = True

# Collation names for use by the Django test suite.
test_collations = {
"ci": None, # Case-insensitive.
Expand Down
11 changes: 9 additions & 2 deletions docs/ref/models/querysets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2774,8 +2774,9 @@ the direction is changed.
*Asynchronous version*: ``afirst()``

Returns the first object matched by the queryset, or ``None`` if there
is no matching object. If the ``QuerySet`` has no ordering defined, then the
queryset is automatically ordered by the primary key. This can affect
is no matching object. If the ``QuerySet`` has no ordering defined (and has not
had ordering forcibly cleared by calling :meth:`order_by` with no arguments),
then the queryset is automatically ordered by the primary key. This can affect
aggregation results as described in :ref:`aggregation-ordering-interaction`.

Example::
Expand All @@ -2790,6 +2791,12 @@ equivalent to the above example::
except IndexError:
p = None

.. versionchanged:: 6.1

``first()`` and :meth:`last` no longer order by the primary key when
ordering has been forcibly cleared by calling :meth:`order_by` with no
arguments.

``last()``
~~~~~~~~~~

Expand Down
7 changes: 7 additions & 0 deletions docs/releases/6.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ backends.
``db_on_delete``) as values. ``db_on_delete`` is one of the database-level
delete options e.g. :attr:`~django.db.models.DB_CASCADE`.

* Set the new ``DatabaseFeatures.supports_inspectdb`` attribute to ``False``
if the management command isn't supported.

:mod:`django.contrib.gis`
-------------------------

Expand Down Expand Up @@ -422,6 +425,10 @@ Miscellaneous
``null``, to match the behavior of :lookup:`exact=None <exact>` on key
transforms. Previously, it was interpreted as an :lookup:`isnull` lookup.

* :meth:`~.QuerySet.first` and :meth:`~.QuerySet.last` no longer order by the
primary key when a ``QuerySet``'s ordering has been forcibly cleared by
calling :meth:`~.QuerySet.order_by` with no arguments.

.. _deprecated-features-6.1:

Features deprecated in 6.1
Expand Down
1 change: 1 addition & 0 deletions tests/gis_tests/inspectapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .models import AllOGRFields


@skipUnlessDBFeature("supports_inspectdb")
class InspectDbTests(TestCase):
def test_geom_columns(self):
"""
Expand Down
22 changes: 20 additions & 2 deletions tests/inspectdb/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
from io import StringIO
from unittest import mock, skipUnless

from django.core.management import call_command
from django.core.management import CommandError, call_command
from django.core.management.commands import inspectdb
from django.db import connection
from django.db.backends.base.introspection import TableInfo
from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
from django.test import (
TestCase,
TransactionTestCase,
skipIfDBFeature,
skipUnlessDBFeature,
)

from .models import PeopleMoreData, test_collation

Expand Down Expand Up @@ -40,6 +45,7 @@ def cursor_execute(*queries):
return results


@skipUnlessDBFeature("supports_inspectdb")
class InspectDBTestCase(TestCase):
unique_re = re.compile(r".*unique_together = \((.+),\).*")

Expand Down Expand Up @@ -519,6 +525,7 @@ def test_same_relations(self):
)


@skipUnlessDBFeature("supports_inspectdb")
class InspectDBTransactionalTests(TransactionTestCase):
available_apps = ["inspectdb"]

Expand Down Expand Up @@ -671,3 +678,14 @@ def test_composite_primary_key_not_unique_together(self):
out = StringIO()
call_command("inspectdb", "inspectdb_compositepkmodel", stdout=out)
self.assertNotIn("unique_together", out.getvalue())


@skipIfDBFeature("supports_inspectdb")
class InspectDBNotSupportedTests(TestCase):
def test_not_supported(self):
msg = (
"Database inspection isn't supported for the currently selected "
"database backend."
)
with self.assertRaisesMessage(CommandError, msg):
call_command("inspectdb")