From: Mike Bayer Date: Fri, 20 Jun 2014 22:03:28 +0000 (-0400) Subject: - The :paramref:`.Column.nullable` flag is implicitly set to ``False`` X-Git-Tag: rel_1_0_0b1~387 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bd56485f4c156e9c5ffc911d9119803b1de3789f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - 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. fixes #3023 --- diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index 1f58a0d845..e0a2b02a99 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -14,6 +14,18 @@ .. 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 diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 9ff022f60d..13461f3f3a 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -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): diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index c078babff1..02d8e65ed7 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -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