Gregg Lind wrote:
>
> I used the DDL style
>
> DDL('''ALTER TABLE data ADD CONSTRAINT lowername_check CHECK
> (lowername !~ '[[\:upper\:]]')''',
> on="postgres").execute_at('after-create',Data.__table__)
>
> and now my "print_schema" method (based on
> http://www.sqlalchemy.org/trac/wiki/FAQ#HowcanIgettheCREATETABLEDROPTABLEoutputasastring)
> breaks (on PG only, because of the DDL), with this error:
>
> TypeError: unsupported operand type(s) for +: '_TextClause' and 'str'
>
> I escaped the colons in the DDL. Workarounds?
escape them with \\: or use r'\:'
>
> Gregg
>
>
> Code:
>
> def print_schema(T="postgres"):
> ''' print print_schema will print the schema in use '''
> global Base
> from StringIO import StringIO
> buf = StringIO()
> print '%s://' % T
> engine = create_engine('%s://' % T, strategy='mock',
> executor=lambda s, p='': buf.write(s + p))
> Base.metadata.create_all(engine)
> return buf.getvalue()
>
>
>
> On Fri, May 29, 2009 at 12:27 PM, Gregg Lind <[email protected]> wrote:
>> As always, thank you for the complete, exhaustive answer. This
>> particular thing is definitely and edge case, and rather non-obvious,
>> so thank you for walking me through it.
>>
>> Either of those are clean enough for me!
>>
>> Is there are more proper / general way to describe the problem, so
>> google and make this answer easier to find?
>>
>> Gregg
>>
>>
>>
>> On Fri, May 29, 2009 at 12:10 PM, Michael Bayer
>> <[email protected]> wrote:
>>>
>>> Gregg Lind wrote:
>>>>
>>>> I use declarative base for defining classes.
>>>>
>>>> I have a constraint that only works in Postgres. How do I declare
>>>> that constraint "lowername_check" only if the session is going
>>>> postgres (and not to sqlite, for example).
>>>>
>>>> pg_only_constraint = CheckConstraint("lowername !~
>>>> '[[:upper:]]'",name='lowername_check'),
>>>> class Data(Base):
>>>> __tablename__ = 'Data'
>>>> lowername=Column(Unicode, nullable=False)
>>>> __table_args__ = (
>>>> pg_only_constraint, {}
>>>> )
>>>
>>>
>>>
>>> The cleanest way is to use the schema.DDL() construct which can filter
>>> against various backends, but requires that you spell out the
>>> constraint
>>> explicitly:
>>>
>>> DDL("CREATE CONSTRAINT ....", on="postgres").execute_at('after-create',
>>> Data.__table__)
>>>
>>> Alternatively, if you want to stick with the CheckConstraint object you
>>> can create a function "create_pg_constraints()" which is called at the
>>> point your app calls "create_engine()", that contains all PG specific
>>> constructs - the function would be called based on engine.dialect.name
>>> ==
>>> "postgres".
>>>
>>> We have a more flexible architecture in 0.6 for this sort of thing and
>>> I
>>> think if we add an AddConstraint() construct there and also move most
>>> of
>>> DDL()'s execute-at and "on" functionality into the base DDLElement
>>> class,
>>> that would enable both constructs to be combined together as in
>>> AddConstraint(CheckConstraint(...args...),
>>> on="postgres")).execute_at('after-create', Data.__table__).
>>>
>>>
>>>
>>>>
>>>>
>>>> Thanks!
>>>>
>>>> Gregg Lind
>>>>
>>>> >
>>>>
>>>
>>>
>>> >>
>>>
>>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---