COLLATE within TEXT, VARCHAR in CREATE TABLE now supported by PG, SQlite, MySQL

Issue #2276 resolved
Former user created an issue

There seems to be no way to get sqlalchemy to generate DDL setting a default collation for a column (a la MySQL). SQLite supports CREATE TABLE (column TEXT COLLATE <collation>).

I would hope for Column(String(collation='blah')) but even Column(sqlalchemy.TEXT(collation='blah')) would be nice.

Comments (7)

  1. Mike Bayer repo owner

    Here's a workaround for now:

    from sqlalchemy import VARCHAR
    from sqlalchemy.ext.compiler import compiles
    
    class SQLiteString(VARCHAR):
        def __init__(self, length, collation=None):
            super(VARCHAR, self).__init__(length)
            self.collation = collation
    
    @compiles(SQLiteString)
    def _varchar(element, compiler, **kw):
        length = element.length
        collation = element.collation
        if collation:
            return "VARCHAR(%d) COLLATE %s" % (length, collation)
        else:
            return "VARCHAR(%d)" % length
    
  2. Former user Account Deleted

    Thank you!

    I used something very similar (I made it so length was optional), and this seems to work for us.

    Cheers.

  3. Log in to comment