add Table.create_column_by_example method

This adds a method to create a column by giving an example of
the data it will contain rather than by specifying its
SQLAlchemy type.  This extends the range of operations that
can be performed without reading the SQLAlchemy docs.
This commit is contained in:
Paul Fitzpatrick
2016-10-26 09:29:55 -04:00
parent 6fc8bfed51
commit 94bc6e09c0
2 changed files with 24 additions and 1 deletions
+11
View File
@@ -310,6 +310,17 @@ class Table(object):
finally:
self.database._release()
def create_column_by_example(self, name, value):
"""
Explicitly create a new column ``name`` with a type that is appropriate to store
the given example ``value``. The type is guessed in the same way as for the
insert method with ``ensure=True``. If a column of the same name already exists,
no action is taken, even if it is not of the type we would have created.
table.create_column_by_example('length', 4.2)
"""
self._ensure_columns({name: value}, {})
def drop_column(self, name):
"""
Drop the column ``name``.