Support disabling auto-increment on primary keys, fixes #351.

This commit is contained in:
Friedrich Lindenberg 2020-12-29 20:03:45 +01:00
parent 2b1947e407
commit 02d3d0333f
3 changed files with 14 additions and 9 deletions

View File

@ -184,7 +184,9 @@ class Database(object):
except ValueError: except ValueError:
return False return False
def create_table(self, table_name, primary_id=None, primary_type=None): def create_table(
self, table_name, primary_id=None, primary_type=None, primary_increment=None
):
"""Create a new table. """Create a new table.
Either loads a table or creates it if it doesn't exist yet. You can Either loads a table or creates it if it doesn't exist yet. You can
@ -192,7 +194,8 @@ class Database(object):
be created. The default is to create an auto-incrementing integer, be created. The default is to create an auto-incrementing integer,
``id``. You can also set the primary key to be a string or big integer. ``id``. You can also set the primary key to be a string or big integer.
The caller will be responsible for the uniqueness of ``primary_id`` if The caller will be responsible for the uniqueness of ``primary_id`` if
it is defined as a text type. it is defined as a text type. You can disable auto-increment behaviour
for numeric primary keys by setting `primary_increment` to `False`.
Returns a :py:class:`Table <dataset.Table>` instance. Returns a :py:class:`Table <dataset.Table>` instance.
:: ::
@ -223,6 +226,7 @@ class Database(object):
table_name, table_name,
primary_id=primary_id, primary_id=primary_id,
primary_type=primary_type, primary_type=primary_type,
primary_increment=primary_increment,
auto_create=True, auto_create=True,
) )
return self._tables.get(table_name) return self._tables.get(table_name)

View File

@ -31,6 +31,7 @@ class Table(object):
table_name, table_name,
primary_id=None, primary_id=None,
primary_type=None, primary_type=None,
primary_increment=None,
auto_create=False, auto_create=False,
): ):
"""Initialise the table from database schema.""" """Initialise the table from database schema."""
@ -43,6 +44,9 @@ class Table(object):
primary_id if primary_id is not None else self.PRIMARY_DEFAULT primary_id if primary_id is not None else self.PRIMARY_DEFAULT
) )
self._primary_type = primary_type if primary_type is not None else Types.integer self._primary_type = primary_type if primary_type is not None else Types.integer
if primary_increment is None:
primary_increment = self._primary_type in (Types.integer, Types.bigint)
self._primary_increment = primary_increment
self._auto_create = auto_create self._auto_create = auto_create
@property @property
@ -330,14 +334,11 @@ class Table(object):
if self._primary_id is not False: if self._primary_id is not False:
# This can go wrong on DBMS like MySQL and SQLite where # This can go wrong on DBMS like MySQL and SQLite where
# tables cannot have no columns. # tables cannot have no columns.
primary_id = self._primary_id
primary_type = self._primary_type
increment = primary_type in [Types.integer, Types.bigint]
column = Column( column = Column(
primary_id, self._primary_id,
primary_type, self._primary_type,
primary_key=True, primary_key=True,
autoincrement=increment, autoincrement=self._primary_increment,
) )
self._table.append_column(column) self._table.append_column(column)
for column in columns: for column in columns: