]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
The ``default`` argument of :class:`.Column` now accepts a class
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 11 Oct 2013 20:16:08 +0000 (16:16 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 11 Oct 2013 20:16:08 +0000 (16:16 -0400)
or object method as an argument, in addition to a standalone function;
will properly detect if the "context" argument is accepted or not.

doc/build/changelog/changelog_09.rst
lib/sqlalchemy/sql/schema.py
test/sql/test_defaults.py

index c784a39ebe26a297a9eff79c487a9aa8ff214875..0d8df3b7f605a9d6d8360ab1da62d58b02325bb6 100644 (file)
 .. changelog::
     :version: 0.9.0
 
+    .. change::
+        :tags: feature, sql
+
+        The ``default`` argument of :class:`.Column` now accepts a class
+        or object method as an argument, in addition to a standalone function;
+        will properly detect if the "context" argument is accepted or not.
+
     .. change::
         :tags: bug, sql
         :tickets: 2835
index c493b9132088f48f1c1b08f95d015b35615d9d65..fe331ce2ee7ed4a2631a18ee8242653334192a3e 100644 (file)
@@ -1776,7 +1776,7 @@ class ColumnDefault(DefaultGenerator):
         on everyone.
 
         """
-        if inspect.isfunction(fn):
+        if inspect.isfunction(fn) or inspect.ismethod(fn):
             inspectable = fn
         elif inspect.isclass(fn):
             inspectable = fn.__init__
index 1508c0532b146f10f0081622cde9ff2ef28a6646..56b7971b2616dc26a8ad15655f15919472007cf1 100644 (file)
@@ -48,6 +48,11 @@ class DefaultTest(fixtures.TestBase):
         use_function_defaults = testing.against('postgresql', 'mssql', 'maxdb')
         is_oracle = testing.against('oracle')
 
+        class MyClass(object):
+            @classmethod
+            def gen_default(cls, ctx):
+                return "hi"
+
         # select "count(1)" returns different results on different DBs also
         # correct for "current_date" compatible as column default, value
         # differences
@@ -125,7 +130,12 @@ class DefaultTest(fixtures.TestBase):
             # combo
             Column('col9', String(20),
                    default='py',
-                   server_default='ddl'))
+                   server_default='ddl'),
+
+            # python method w/ context
+            Column('col10', String(20), default=MyClass.gen_default)
+        )
+
         t.create()
 
     @classmethod
@@ -285,7 +295,7 @@ class DefaultTest(fixtures.TestBase):
         today = datetime.date.today()
         eq_(l.fetchall(), [
             (x, 'imthedefault', f, ts, ts, ctexec, True, False,
-             12, today, 'py')
+             12, today, 'py', 'hi')
             for x in range(51, 54)])
 
         t.insert().execute(col9=None)
@@ -295,7 +305,7 @@ class DefaultTest(fixtures.TestBase):
 
         eq_(t.select(t.c.col1 == 54).execute().fetchall(),
             [(54, 'imthedefault', f, ts, ts, ctexec, True, False,
-              12, today, None)])
+              12, today, None, 'hi')])
 
     @testing.fails_on('firebird', 'Data type unknown')
     def test_insertmany(self):
@@ -311,11 +321,11 @@ class DefaultTest(fixtures.TestBase):
         today = datetime.date.today()
         eq_(l.fetchall(),
             [(51, 'imthedefault', f, ts, ts, ctexec, True, False,
-              12, today, 'py'),
+              12, today, 'py', 'hi'),
              (52, 'imthedefault', f, ts, ts, ctexec, True, False,
-              12, today, 'py'),
+              12, today, 'py', 'hi'),
              (53, 'imthedefault', f, ts, ts, ctexec, True, False,
-              12, today, 'py')])
+              12, today, 'py', 'hi')])
 
     def test_no_embed_in_sql(self):
         """Using a DefaultGenerator, Sequence, DefaultClause
@@ -379,11 +389,11 @@ class DefaultTest(fixtures.TestBase):
         today = datetime.date.today()
         eq_(l.fetchall(),
             [(51, 'im the update', f2, ts, ts, ctexec, False, False,
-              13, today, 'py'),
+              13, today, 'py', 'hi'),
              (52, 'im the update', f2, ts, ts, ctexec, True, False,
-              13, today, 'py'),
+              13, today, 'py', 'hi'),
              (53, 'im the update', f2, ts, ts, ctexec, True, False,
-              13, today, 'py')])
+              13, today, 'py', 'hi')])
 
     @testing.fails_on('firebird', 'Data type unknown')
     def test_update(self):
@@ -395,7 +405,7 @@ class DefaultTest(fixtures.TestBase):
         l = l.first()
         eq_(l,
             (pk, 'im the update', f2, None, None, ctexec, True, False,
-             13, datetime.date.today(), 'py'))
+             13, datetime.date.today(), 'py', 'hi'))
         eq_(11, f2)
 
     @testing.fails_on('firebird', 'Data type unknown')