declarative_base and UNIQUE Constraint

1,294 views
Skip to first unread message

GustaV

unread,
Sep 14, 2008, 1:49:07 PM9/14/08
to sqlalchemy
How do I create a unique constraint with the declarative plugin
(latest version 0.5) ?

both:
__table_args__ = ( UniqueConstraint('region.x', 'region.y'),
{'mysql_engine':'InnoDB'} )
__table_args__ = ( UniqueConstraint(x, y), {'mysql_engine':'InnoDB'} )
don't work.

Thanks!

Michael Bayer

unread,
Sep 14, 2008, 7:51:54 PM9/14/08
to sqlal...@googlegroups.com
format is __table_args__ = ((UniqueConstraint(....),), {})

FrankB

unread,
Dec 20, 2008, 2:21:45 AM12/20/08
to sqlal...@googlegroups.com
Hi,

just for anyone arriving here to save some time: I tried this with
0.5rc4 and the following piece of code

===

from sqlalchemy import types as satypes
from sqlalchemy import schema as saschema
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
class MasterEntity(Base):
__tablename__ = "master"
id = saschema.Column(satypes.Integer, primary_key=True)
key = saschema.Column(satypes.Unicode(16))
entitytype = saschema.Column(satypes.String(32))
__mapper_args__ = {'polymorphic_on': entitytype,
'polymorphic_identity': 'master'}
__table_args__ = ((saschema.UniqueConstraint(entitytype, key),),
{})

===

and received the error

"AttributeError: 'tuple' object has no attribute '_set_parent'".


Changing the last line to

__table_args__ = (saschema.UniqueConstraint(entitytype, key), {})

(means: removing the tuple) yields

KeyError: Column('entitytype', ...)

but this (means: put column names into quotes) eventually works:

__table_args__ = ( saschema.UniqueConstraint("entitytype",
"key"), {} )


Regards, Frank

---------- Forwarded message ----------
From: Michael Bayer <mike...@zzzcomputing.com>
Date: Sep 15, 12:51 am
Subject: declarative_base and UNIQUE Constraint
To: sqlalchemy


format is __table_args__ = ((UniqueConstraint(....),), {})

On Sep 14, 2008, at 1:49 PM, GustaV wrote:

> How do I create a unique constraint with the declarative plugin
> (latest version 0.5) ?

> both:
> __table_args__ = (UniqueConstraint('region.x', 'region.y'),
> {'mysql_engine':'InnoDB'} )
> __table_args__ = (UniqueConstraint(x, y), {'mysql_engine':'InnoDB'} )
> don't work.

> Thanks!

Reply all
Reply to author
Forward
0 new messages