I'm afraid it just doesn't work. I would think you have to import String 
too..

from sqlalchemy.types import String

The only difference between your and my code, is i don't have a default and 
a sqlite.. I tried it as default, and oracle... not both.
Also the custom function is in a separate module that I load into my main 
one.. But it should not make an iota of difference.
Python 3.6.4
Sqlalchemy 1.2.2
sqlacodegen-1.1.6 to generate a module of domain classes, in which I 
override 
__repr__ on Base ( which is an implementation of 
whatever  declarative_base() returns.


    return compiler.SQLCompiler.visit_select(self, select, **kwargs)
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\compiler.py",
 
line 1807, in visit_select
    text, select, inner_columns, froms, byfrom, kwargs)
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\compiler.py",
 
line 1886, in _compose_select_body
    for f in froms])
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\compiler.py",
 
line 1885, in <listcomp>
    [f._compiler_dispatch(self, asfrom=True, **kwargs)
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\visitors.py",
 
line 81, in _compiler_dispatch
    return meth(self, **kw)
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\dialects\oracle\base.py",
 
line 918, in visit_select
    return compiler.SQLCompiler.visit_select(self, select, **kwargs)
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\compiler.py",
 
line 1785, in visit_select
    for name, column in select._columns_plus_names
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\compiler.py",
 
line 1779, in <listcomp>
    self._label_select_column(
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\compiler.py",
 
line 1557, in _label_select_column
    **column_clause_args
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\visitors.py",
 
line 81, in _compiler_dispatch
    return meth(self, **kw)
  File 
"C:\opt\python\python-3.6.4-64-bit\lib\site-packages\sqlalchemy\sql\compiler.py",
 
line 683, in visit_label
    OPERATORS[operators.as_] + \
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'


On Saturday, 10 February 2018 21:37:08 UTC, Jonathan Vanasco wrote:
>
> The following works for me on Python 2&3
>
> how are you generating an error ?
>
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
> #
> # Use this file to build your own SSCCE
> # SSCCE = Short, Self Contained, Correct (Compatible) Example
> # see http://sscce.org/
> #
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
> # Standard imports
>
> import sqlalchemy
> import sqlalchemy.orm
>
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy import Boolean, Integer, Column, Unicode, ForeignKey, 
> String
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
> # You probably don't need to overwrite this
> Base = declarative_base()
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
> # Define some models that inherit from Base
>
> class Foo(Base):
>     __tablename__ = 'foo'
>     id = Column(Integer, primary_key=True)
>     name = Column(Unicode)
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
> # set the engine
>
> engine = sqlalchemy.create_engine('sqlite:///:memory:', echo=True)
> Base.metadata.create_all(engine)
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
> # do a simple query to trigger the mapper error
>
> sessionFactory = sqlalchemy.orm.sessionmaker(bind=engine)
> s = sessionFactory()
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
> - - -
> # test
>
> from sqlalchemy.sql import expression
> from sqlalchemy.ext.compiler import compiles
>
>
> class stripctrl(expression.FunctionElement):
>     type = String()
>     name = 'stripctrl'
>
>
> @compiles(stripctrl)
> def stripctrl_default(element, compiler, **kw):
>     args = list(element.clauses)
>     return "REPLACE(REPLACE(REPLACE(%s, CHR(9), ''), CHR(10), ''), 
> CHR(13), '')" % (compiler.process(args[0]))
>
>
> @compiles(stripctrl, 'sqlite')
> def stripctrl_sqlite(element, compiler, **kw):
>     args = list(element.clauses)
>     return "replace(replace(replace(%s, char(9), ''), char(10), ''), 
> char(13), '')" % (compiler.process(args[0]))
>
>
> cases = (None, "Aaaa", 123, 'A\ta', 'B\rb', 'C\nc,')
> for idx, case in enumerate(cases):
>     f = Foo()
>     f.id = idx
>     f.name = case
>     s.add(f)
> s.commit()
>     
> print("--SELECT")
> q = s.query(stripctrl(Foo.name)).all()
> print (q)
>
> print ("-- INSERT")
> for idx, case in enumerate(cases):
>     f = Foo()
>     f.id = idx + 100
>     f.name = stripctrl(case)
>     s.add(f)
> s.commit()
>
>
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to