#30511: Support Identity columns on PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: Michael Kany | Owner: Michael
| Kany
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: postgres generated | Triage Stage: Accepted
identity |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by nbngn):
Hi, I am also interested in having IDENTITY as Autofield in PostgreSQL. I
have attempted to implement it the past few weeks according to the
specifications mentioned here but it's not so easy.
1. I was only able to write an exception for checking if the PostgreSQL
version is >=10. I built it like the check if brin_autosummarize is
allowed. Which is I will push once I have the rest of code with IDENTITY
implemented (which will take a while). **What are the requirements to drop
support for PostgreSQL < 10?**
Built similarly to:
[https://github.com/django/django/blob/7254f1138d9c51fa558229c39c9559b369c4278a/django/db/backends/postgresql/features.py#L68
has_brin_autosummarize in db\backends\postgresql\features.py]
[https://github.com/django/django/blob/85ac838d9e6975130b5c55299e9d7d1222a8e289/django/contrib/postgres/indexes.py#L57
check_support has_brin_autosummarize in contrib\postgres\indexes.py]
{{{
#!python
# in db\backends\postgresql\base.py:
def check_supported(self):
"""
Using IDENTITY is supported in since Django 3.0, PostgreSQL>=10.
Using SERIAL is supported in Django Version<3.0, PostgreSQL<10.
"""
if not self.features_class.changed_serial_to_identity:
raise NotSupportedError('AutoField requires PostgreSQL 10+.
Using SERIAL is supported in Django Version<3.0.')
# in db\backends\postgresql\features.py:
changed_serial_to_identity =
property(operator.attrgetter('is_postgresql_10'))
# in tests\backends\postgresql\tests.py
def test_database_supported_version(self):
"""
Fundamental changes in AutoField requires PostgreSQL 10+.
SQL-compliant GENERATED BY IDENTITY is supported instead of
SERIAL.
"""
from django.db.backends.postgresql.base import DatabaseWrapper
new_connection = connection.copy()
db_wrapper = DatabaseWrapper(new_connection.settings_dict)
unsupported_version = new_connection.pg_version < 100000
# Test unsupported version
msg = 'AutoField requires PostgreSQL 10+. Using SERIAL is
supported in Django Version<3.0.'
with self.assertRaisesMessage(NotSupportedError, msg):
db_wrapper.features_class.changed_serial_to_identity =
unsupported_version
db_wrapper.check_supported()
# After test should reset back to original state
db_wrapper.features_class.changed_serial_to_identity =
property(operator.attrgetter('db_wrapper.features_class.is_postgresql_10'))
self.assertTrue(db_wrapper.features_class.changed_serial_to_identity !=
unsupported_version)
}}}
2. List of where changes need to be made that I have found so far:
||=location=||=method=||=necessary changes=||
||django/db/backends/postgresql/base.py ||DatabaseWrapper.data_types||^^*
serial → int GENERATED BY DEFAULT AS IDENTITY\\ ^^* bigserial → bigint
GENERATED BY DEFAULT AS IDENTITY\\ ^^* smallserial → smallint GENERATED BY
DEFAULT AS IDENTITY\\||
||django/db/backends/postgresql/operations.py
||DatabaseOperations.sequence_reset_by_name_sql
(pg_get_serial_sequence)||need to write a function to find the sequence of
an identity field||
||django/db/backends/postgresql/operations.py
||DatabaseOperations.sequence_reset_sql (pg_get_serial_sequence)||need to
write a function to find the sequence of an identity field||
||django/db/backends/postgresql/schema.py
||DatabaseSchemaEditor._alter_column_type_sql||alterations for IDENTITY||
Found unit-test locations:
||=location=||
||django/tests/db/backends/base/||
||django/tests/db/backends/postgresql/||
||django/tests/schema/||
||django/tests/postgres_tests/||
I think for the sequences no real changes need to be made. Just all with
the SERIAL-only parts. Are these all locations?
--
Ticket URL: <https://code.djangoproject.com/ticket/30511#comment:16>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/068.97d42e76e794ffff4d7ad36b598d8823%40djangoproject.com.