]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
test transfer of default, insert_default
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 3 Jul 2022 17:55:20 +0000 (13:55 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 3 Jul 2022 17:55:20 +0000 (13:55 -0400)
right now "default" goes to Column.default unconditionally
if insert_default is not present, including if dataclasses
are in use where the field effectively now does two things.
This generally works out because Python side default
can be assigned to the object or picked up by Core in any case.
However, we might want to look into later on migrating this
to have the fields act more separately.  I think it's
"OK" for now, will try to doc that this might change.

Change-Id: I30f2085ec79a6464da4d5c578500848c70d55ec2

test/orm/declarative/test_typed_mapping.py

index beb5d783bf4ede070a7bc5fb9fb09a08ea540f1b..4a7b169cd11c74dbec5f49c9e5b42ad7fb158831 100644 (file)
@@ -87,6 +87,25 @@ class DeclarativeBaseTest(fixtures.TestBase):
 class MappedColumnTest(fixtures.TestBase, testing.AssertsCompiledSQL):
     __dialect__ = "default"
 
+    @testing.combinations(
+        "default", "insert_default", argnames="use_paramname"
+    )
+    @testing.combinations(True, False, argnames="use_none")
+    def test_col_defaults(self, use_paramname, use_none, decl_base):
+        class Foo(decl_base):
+            __tablename__ = "foo"
+
+            id: Mapped[int] = mapped_column(primary_key=True)
+
+            data: Mapped[int] = mapped_column(
+                **{use_paramname: None if use_none else 5}
+            )
+
+        if use_none:
+            assert not Foo.__table__.c.data.default
+        else:
+            eq_(Foo.__table__.c.data.default.arg, 5)
+
     def test_legacy_declarative_base(self):
         typ = VARCHAR(50)
         Base = declarative_base(type_annotation_map={str: typ})