Hi,

I was trying to define a subclass of CheckConstraint to match a column 
(whose name is given) to a regexp.

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_-]+$"))

This ends up failing because the regexp is stored using a bind parameter 
but then lost somewhere along the way. Here is a traceback:

  File "/home/luca/Development/cms/cms/db/init.py", line 34, in init_db
    metadata.create_all()
  File "/usr/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 
3860, in create_all
    tables=tables)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 
1920, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 
1529, in _run_visitor
    **kwargs).traverse_single(element)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 
121, in traverse_single
    return meth(obj, **kw)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 733, 
in visit_metadata
    _is_metadata_operation=True)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 
121, in traverse_single
    return meth(obj, **kw)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 767, 
in visit_table
    include_foreign_key_constraints=include_foreign_key_constraints
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 
945, in execute
    return meth(self, multiparams, params)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py", line 68, 
in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 
1002, in _execute_ddl
    compiled
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 
1189, in _execute_context
    context)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 
1393, in _handle_dbapi_exception
    exc_info
  File "/usr/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 
203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 
1182, in _execute_context
    context)
  File "/usr/lib/python2.7/site-packages/sqlalchemy/engine/default.py", 
line 470, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) syntax error 
at or near ":"
LINE 4:  name VARCHAR NOT NULL CHECK (name ~ :name_1), 

I am using SQLAlchemy 1.1.6 with PostgreSQL. Am I doing anything wrong or 
is this a bug?

Thanks!

PS: I know I can use a string, like "%s ~ '...'" % column_name, but in 
other cases the regexps are more complicated and I don't want to worry 
about escaping.
PS: I also know that I can add the constraint using __table_args__ at the 
end of the class definition, passing the actual Column object, but I prefer 
the constraint to be given to the Column constructor to keep it local.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to