]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The :paramref:`.Column.nullable` flag is implicitly set to ``False``
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 Jun 2014 22:03:28 +0000 (18:03 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 Jun 2014 22:03:28 +0000 (18:03 -0400)
when that :class:`.Column` is referred to in an explicit
:class:`.PrimaryKeyConstraint` for that table.  This behavior now
matches that of when the :class:`.Column` itself has the
:paramref:`.Column.primary_key` flag set to ``True``, which is
intended to be an exactly equivalent case.
fixes #3023

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

index 1f58a0d8456d88a0a53c68996c2c3385d6b38332..e0a2b02a995c9ea8844b4741697718ee38a658be 100644 (file)
 .. changelog::
     :version: 0.9.5
 
+    .. change::
+        :tags: bug, sql
+        :tickets: 3023
+        :versions: 1.0.0
+
+        The :paramref:`.Column.nullable` flag is implicitly set to ``False``
+        when that :class:`.Column` is referred to in an explicit
+        :class:`.PrimaryKeyConstraint` for that table.  This behavior now
+        matches that of when the :class:`.Column` itself has the
+        :paramref:`.Column.primary_key` flag set to ``True``, which is
+        intended to be an exactly equivalent case.
+
     .. change::
         :tags: enhancement, postgresql
         :tickets: 3002
index 9ff022f60d3f46aea7db88bc7c5705d0fac36892..13461f3f3a4321790f05f4959dd8c06818e4c64d 100644 (file)
@@ -2652,6 +2652,7 @@ class PrimaryKeyConstraint(ColumnCollectionConstraint):
 
         for c in self.columns:
             c.primary_key = True
+            c.nullable = False
         self.columns.extend(table_pks)
 
     def _reload(self, columns):
index c078babff1e9402b4f9f066bcbec0e86bfdc52e6..02d8e65ed7afb88fc7f8b31c5b5b59706c777b94 100644 (file)
@@ -1053,6 +1053,24 @@ class TableTest(fixtures.TestBase, AssertsCompiledSQL):
         )
         eq_(list(t.primary_key), [t.c.b, t.c.c])
 
+    def test_pk_always_flips_nullable(self):
+        m = MetaData()
+
+        t1 = Table('t1', m, Column('x', Integer), PrimaryKeyConstraint('x'))
+
+        t2 = Table('t2', m, Column('x', Integer, primary_key=True))
+
+        eq_(list(t1.primary_key), [t1.c.x])
+
+        eq_(list(t2.primary_key), [t2.c.x])
+
+        assert t1.c.x.primary_key
+        assert t2.c.x.primary_key
+
+        assert not t2.c.x.nullable
+        assert not t1.c.x.nullable
+
+
 class SchemaTypeTest(fixtures.TestBase):
     class MyType(sqltypes.SchemaType, sqltypes.TypeEngine):
         column = None