From 6f99bdf013f3a0637f0544c4c3daeac0392553d6 Mon Sep 17 00:00:00 2001 From: Gord Thompson Date: Thu, 21 Nov 2019 09:43:40 -0500 Subject: Add sequence support for MariaDB 10.3+. Added support for use of the :class:`.Sequence` construct with MariaDB 10.3 and greater, as this is now supported by this database. The construct integrates with the :class:`.Table` object in the same way that it does for other databases like PostrgreSQL and Oracle; if is present on the integer primary key "autoincrement" column, it is used to generate defaults. For backwards compatibility, to support a :class:`.Table` that has a :class:`.Sequence` on it to support sequence only databases like Oracle, while still not having the sequence fire off for MariaDB, the optional=True flag should be set, which indicates the sequence should only be used to generate the primary key if the target database offers no other option. Fixes: #4976 Closes: #4996 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4996 Pull-request-sha: cb2e1426ea0b6bc6c93dbe8f033a11df9d8c4915 Change-Id: I507bc405eee6cae2c5991345d0eac53a37fe7512 --- lib/sqlalchemy/sql/crud.py | 12 ++++++++++-- lib/sqlalchemy/sql/schema.py | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/crud.py b/lib/sqlalchemy/sql/crud.py index 881ea9fcd..58abc10df 100644 --- a/lib/sqlalchemy/sql/crud.py +++ b/lib/sqlalchemy/sql/crud.py @@ -534,13 +534,21 @@ def _append_param_insert_pk(compiler, stmt, c, values, kw): no value passed in either; raise an exception. """ + if ( # column has a Python-side default c.default is not None and ( - # and it won't be a Sequence + # and it either is not a sequence, or it is and we support + # sequences and want to invoke it not c.default.is_sequence - or compiler.dialect.supports_sequences + or ( + compiler.dialect.supports_sequences + and ( + not c.default.optional + or not compiler.dialect.sequences_optional + ) + ) ) ) or ( # column is the "autoincrement column" diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 8c325538c..c7d699a5f 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2281,6 +2281,8 @@ class Sequence(roles.StatementRole, DefaultGenerator): .. seealso:: + :ref:`defaults_sequences` + :class:`.CreateSequence` :class:`.DropSequence` -- cgit v1.2.1