Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect rendering of sqltext for column-level check constraint #3957

Closed
sqlalchemy-bot opened this issue Apr 4, 2017 · 2 comments
Closed
Labels
bug Something isn't working sql
Milestone

Comments

@sqlalchemy-bot
Copy link
Collaborator

Migrated issue, originally created by Michael Bayer (@zzzeek)

from sqlalchemy import *
from sqlalchemy.sql.expression import ColumnClause

class CodenameConstraint(CheckConstraint):
    """Check that the column uses a limited alphabet."""
    def __init__(self, column_name):
        column = ColumnClause(column_name)
        super(CodenameConstraint, self).__init__(
            column.op("~")("^[A-Za-z0-9_-]+$"))


m = MetaData()
t = Table(
    't', m, Column('x', String, CodenameConstraint('x')),

)

from sqlalchemy.schema import CreateTable

print CreateTable(t)

a string compiler is being invoked separately, needs this:

diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 4415028..b18f903 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -2602,7 +2602,9 @@ class DDLCompiler(Compiled):
             formatted_name = self.preparer.format_constraint(constraint)
             if formatted_name is not None:
                 text += "CONSTRAINT %s " % formatted_name
-        text += "CHECK (%s)" % constraint.sqltext
+        text += "CHECK (%s)" % self.sql_compiler.process(constraint.sqltext,
+                                                         include_table=False,
+                                                         literal_binds=True)
         text += self.define_constraint_deferrability(constraint)
         return text
 

been this way for years so 1.2 to allow for any surprises.

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

Apply SQL compilation to sqltext for column-level CHECK constraint

Fixed bug where a column-level :class:.CheckConstraint would fail
to compile the SQL expression using the underlying dialect compiler
as well as apply proper flags to generate literal values as
inline, in the case that the sqltext is a Core expression and
not just a plain string. This was long-ago fixed for table-level
check constraints in 0.9 as part of 🎫2742, which more commonly
feature Core SQL expressions as opposed to plain string expressions.

Change-Id: I1301ba4b40063e91bc47726aecc5f4990ffcaeda
Fixes: #3957

4a4b17e

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot sqlalchemy-bot added bug Something isn't working sql labels Nov 27, 2018
@sqlalchemy-bot sqlalchemy-bot added this to the 1.2 milestone Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sql
Projects
None yet
Development

No branches or pull requests

1 participant