Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sqlalchemy_utils/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def instant_defaults_listener(target, args, kwargs):

for key, column in sa.inspect(target.__class__).columns.items():
if hasattr(column, 'default') and column.default is not None:
if callable(column.default.arg):
if not hasattr(column.default, 'arg'):
continue
elif callable(column.default.arg):
kwargs[key] = column.default.arg(target)
else:
kwargs[key] = column.default.arg
Expand Down
19 changes: 19 additions & 0 deletions tests/test_instant_defaults_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ def byline(self, value):
return Article


@pytest.fixture
def Document(Base):
class Document(Base):
__tablename__ = 'document'
id = sa.Column(
sa.Integer,
sa.Sequence('document_id_seq'),
primary_key=True
)
title = sa.Column(sa.Unicode(255), default='Untitled')

return Document


class TestInstantDefaultListener:
def test_assigns_defaults_on_object_construction(self, Article):
article = Article()
Expand All @@ -40,3 +54,8 @@ def test_callables_as_defaults(self, Article):
def test_override_default_with_setter_function(self, Article):
article = Article(byline='provided byline')
assert article.byline == 'provided byline'

def test_handles_sequence_defaults(self, Document):
document = Document()
assert document.title == 'Untitled'
assert document.id is None