summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-10-11 16:16:08 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-10-11 16:16:08 -0400
commit1f1ad557afa4919245c2cb9972099c1504a30a37 (patch)
tree74fac80835116d55f9e36b7de3e49d6369571308
parent9d38ed33400adf3ba8fdf3af49f26de1270bbe23 (diff)
downloadsqlalchemy-1f1ad557afa4919245c2cb9972099c1504a30a37.tar.gz
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.
-rw-r--r--doc/build/changelog/changelog_09.rst7
-rw-r--r--lib/sqlalchemy/sql/schema.py2
-rw-r--r--test/sql/test_defaults.py30
3 files changed, 28 insertions, 11 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst
index c784a39eb..0d8df3b7f 100644
--- a/doc/build/changelog/changelog_09.rst
+++ b/doc/build/changelog/changelog_09.rst
@@ -13,6 +13,13 @@
: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
diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py
index c493b9132..fe331ce2e 100644
--- a/lib/sqlalchemy/sql/schema.py
+++ b/lib/sqlalchemy/sql/schema.py
@@ -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__
diff --git a/test/sql/test_defaults.py b/test/sql/test_defaults.py
index 1508c0532..56b7971b2 100644
--- a/test/sql/test_defaults.py
+++ b/test/sql/test_defaults.py
@@ -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')