diff --git a/dataset/database.py b/dataset/database.py index a4bf302..dc4f711 100644 --- a/dataset/database.py +++ b/dataset/database.py @@ -184,7 +184,9 @@ class Database(object): except ValueError: 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. 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, ``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 - 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 ` instance. :: @@ -223,6 +226,7 @@ class Database(object): table_name, primary_id=primary_id, primary_type=primary_type, + primary_increment=primary_increment, auto_create=True, ) return self._tables.get(table_name) diff --git a/dataset/table.py b/dataset/table.py index b8e583e..f3f8e3d 100644 --- a/dataset/table.py +++ b/dataset/table.py @@ -31,6 +31,7 @@ class Table(object): table_name, primary_id=None, primary_type=None, + primary_increment=None, auto_create=False, ): """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 ) 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 @property @@ -330,14 +334,11 @@ class Table(object): if self._primary_id is not False: # This can go wrong on DBMS like MySQL and SQLite where # 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( - primary_id, - primary_type, + self._primary_id, + self._primary_type, primary_key=True, - autoincrement=increment, + autoincrement=self._primary_increment, ) self._table.append_column(column) for column in columns: diff --git a/test/test_dataset.py b/test/test_dataset.py index e580999..9c2bb8e 100644 --- a/test/test_dataset.py +++ b/test/test_dataset.py @@ -542,7 +542,7 @@ class TableTestCase(unittest.TestCase): class Constructor(dict): - """ Very simple low-functionality extension to ``dict`` to + """Very simple low-functionality extension to ``dict`` to provide attribute access to dictionary contents""" def __getattr__(self, name):