diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-10-07 10:02:45 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-10-07 10:02:45 -0400 |
| commit | 414af7b61291b3fa77eb6da6a9b123399214089b (patch) | |
| tree | 0d08a583f9ceae522307c725e4ea67fff261b82f /test/sql/test_metadata.py | |
| parent | 4578ab54a5b849fdb94a7032987f105b7ec117a4 (diff) | |
| download | sqlalchemy-414af7b61291b3fa77eb6da6a9b123399214089b.tar.gz | |
- The system by which a :class:`.Column` considers itself to be an
"auto increment" column has been changed, such that autoincrement
is no longer implicitly enabled for a :class:`.Table` that has a
composite primary key. In order to accommodate being able to enable
autoincrement for a composite PK member column while at the same time
maintaining SQLAlchemy's long standing behavior of enabling
implicit autoincrement for a single integer primary key, a third
state has been added to the :paramref:`.Column.autoincrement` parameter
``"auto"``, which is now the default. fixes #3216
- The MySQL dialect no longer generates an extra "KEY" directive when
generating CREATE TABLE DDL for a table using InnoDB with a
composite primary key with AUTO_INCREMENT on a column that isn't the
first column; to overcome InnoDB's limitation here, the PRIMARY KEY
constraint is now generated with the AUTO_INCREMENT column placed
first in the list of columns.
Diffstat (limited to 'test/sql/test_metadata.py')
| -rw-r--r-- | test/sql/test_metadata.py | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 2e51b9a91..24f416439 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -1361,6 +1361,128 @@ class TableTest(fixtures.TestBase, AssertsCompiledSQL): assert not t1.c.x.nullable +class PKAutoIncrementTest(fixtures.TestBase): + def test_multi_integer_no_autoinc(self): + pk = PrimaryKeyConstraint( + Column('a', Integer), + Column('b', Integer) + ) + t = Table('t', MetaData()) + t.append_constraint(pk) + + is_(pk._autoincrement_column, None) + + def test_multi_integer_multi_autoinc(self): + pk = PrimaryKeyConstraint( + Column('a', Integer, autoincrement=True), + Column('b', Integer, autoincrement=True) + ) + t = Table('t', MetaData()) + t.append_constraint(pk) + + assert_raises_message( + exc.ArgumentError, + "Only one Column may be marked", + lambda: pk._autoincrement_column + ) + + def test_single_integer_no_autoinc(self): + pk = PrimaryKeyConstraint( + Column('a', Integer), + ) + t = Table('t', MetaData()) + t.append_constraint(pk) + + is_(pk._autoincrement_column, pk.columns['a']) + + def test_single_string_no_autoinc(self): + pk = PrimaryKeyConstraint( + Column('a', String), + ) + t = Table('t', MetaData()) + t.append_constraint(pk) + + is_(pk._autoincrement_column, None) + + def test_single_string_illegal_autoinc(self): + t = Table('t', MetaData(), Column('a', String, autoincrement=True)) + pk = PrimaryKeyConstraint( + t.c.a + ) + t.append_constraint(pk) + + assert_raises_message( + exc.ArgumentError, + "Column type VARCHAR on column 't.a'", + lambda: pk._autoincrement_column + ) + + def test_single_integer_illegal_default(self): + t = Table( + 't', MetaData(), + Column('a', Integer, autoincrement=True, default=lambda: 1)) + pk = PrimaryKeyConstraint( + t.c.a + ) + t.append_constraint(pk) + + assert_raises_message( + exc.ArgumentError, + "Column default.*on column t.a is not compatible", + lambda: pk._autoincrement_column + ) + + def test_single_integer_illegal_server_default(self): + t = Table( + 't', MetaData(), + Column('a', Integer, + autoincrement=True, server_default=func.magic())) + pk = PrimaryKeyConstraint( + t.c.a + ) + t.append_constraint(pk) + + assert_raises_message( + exc.ArgumentError, + "Column server default.*on column t.a is not compatible", + lambda: pk._autoincrement_column + ) + + def test_implicit_autoinc_but_fks(self): + m = MetaData() + Table('t1', m, Column('id', Integer, primary_key=True)) + t2 = Table( + 't2', MetaData(), + Column('a', Integer, ForeignKey('t1.id'))) + pk = PrimaryKeyConstraint( + t2.c.a + ) + t2.append_constraint(pk) + is_(pk._autoincrement_column, None) + + def test_explicit_autoinc_but_fks(self): + m = MetaData() + Table('t1', m, Column('id', Integer, primary_key=True)) + t2 = Table( + 't2', MetaData(), + Column('a', Integer, ForeignKey('t1.id'), autoincrement=True)) + pk = PrimaryKeyConstraint( + t2.c.a + ) + t2.append_constraint(pk) + is_(pk._autoincrement_column, t2.c.a) + + t3 = Table( + 't3', MetaData(), + Column('a', Integer, + ForeignKey('t1.id'), autoincrement='ignore_fk')) + pk = PrimaryKeyConstraint( + t3.c.a + ) + t3.append_constraint(pk) + is_(pk._autoincrement_column, t3.c.a) + + class SchemaTypeTest(fixtures.TestBase): class MyType(sqltypes.SchemaType, sqltypes.TypeEngine): |
