diff options
| author | cheremnov <32135863+cheremnov@users.noreply.github.com> | 2022-02-24 02:22:33 -0500 |
|---|---|---|
| committer | Federico Caselli <cfederico87@gmail.com> | 2022-06-29 09:13:37 +0000 |
| commit | 5fb63bc1423e75812a24e809d16731a3282c2a12 (patch) | |
| tree | 2e3293890e1b326146ea8848ceac9a65fae9490b /test/sql | |
| parent | 6a560cf03c302d2ebd9ae7c7dc4d587983096ba4 (diff) | |
| download | sqlalchemy-5fb63bc1423e75812a24e809d16731a3282c2a12.tar.gz | |
Comments on (named) constraints
Adds support for comments on named constraints, including `ForeignKeyConstraint`, `PrimaryKeyConstraint`, `CheckConstraint`, `UniqueConstraint`, solving the [Issue 5667](https://github.com/sqlalchemy/sqlalchemy/issues/5667).
Supports only PostgreSQL backend.
### Description
Following the example of [Issue 1546](https://github.com/sqlalchemy/sqlalchemy/issues/1546), supports comments on constraints. Specifically, enables comments on _named_ ones — as I get it, PostgreSQL prohibits comments on unnamed constraints.
Enables setting the comments for named constraints like this:
```
Table(
'example', metadata,
Column('id', Integer),
Column('data', sa.String(30)),
PrimaryKeyConstraint(
"id", name="id_pk", comment="id_pk comment"
),
CheckConstraint('id < 100', name="cc1", comment="Id value can't exceed 100"),
UniqueConstraint(['data'], name="uc1", comment="Must have unique data field"),
)
```
Provides the DDL representation for constraint comments and routines to create and drop them. Class `.Inspector` reflects constraint comments via methods like `get_check_constraints` .
### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)
-->
This pull request is:
- [ ] A documentation / typographical error fix
- [ ] A short code fix
- [x] A new feature implementation
- Solves the issue 5667.
- The commit message includes `Fixes: 5667`.
- Includes tests based on comment reflection.
**Have a nice day!**
Fixes: #5667
Closes: #7742
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7742
Pull-request-sha: 42a5d3c3e9ccf9a9d5397fd007aeab0854f66130
Change-Id: Ia60f578595afdbd6089541c9a00e37997ef78ad3
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_metadata.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 1a070fcf5..33b6e130f 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -237,6 +237,7 @@ class MetaDataTest(fixtures.TestBase, ComparesTables): deferrable="Z", initially="Q", link_to_name=True, + comment="foo", ) fk1 = ForeignKey(c1, **kw) @@ -259,6 +260,7 @@ class MetaDataTest(fixtures.TestBase, ComparesTables): name="name", initially=True, deferrable=True, + comment="foo", _create_rule=r, ) c2 = c._copy() @@ -266,6 +268,7 @@ class MetaDataTest(fixtures.TestBase, ComparesTables): eq_(str(c2.sqltext), "foo bar") eq_(c2.initially, True) eq_(c2.deferrable, True) + eq_(c2.comment, "foo") assert c2._create_rule is r def test_col_replace_w_constraint(self): @@ -3577,6 +3580,35 @@ class ConstraintTest(fixtures.TestBase): for c in t3.constraints: assert c.table is t3 + def test_ColumnCollectionConstraint_copy(self): + m = MetaData() + + t = Table("tbl", m, Column("a", Integer), Column("b", Integer)) + t2 = Table("t2", m, Column("a", Integer), Column("b", Integer)) + + kw = { + "comment": "baz", + "name": "ccc", + "initially": "foo", + "deferrable": "bar", + } + + UniqueConstraint(t.c.a, **kw) + CheckConstraint(t.c.a > 5, **kw) + ForeignKeyConstraint([t.c.a], [t2.c.a], **kw) + PrimaryKeyConstraint(t.c.a, **kw) + + m2 = MetaData() + + t3 = t.to_metadata(m2) + + eq_(len(t3.constraints), 4) + + for c in t3.constraints: + assert c.table is t3 + for k, v in kw.items(): + eq_(getattr(c, k), v) + def test_check_constraint_copy(self): m = MetaData() t = Table("tbl", m, Column("a", Integer), Column("b", Integer)) |
