On 05/03/2013 04:18 PM, Simon King wrote:
On Thu, May 2, 2013 at 11:43 PM, Paradox <para...@pobox.com> wrote:
CREATE TABLE user (lname string, fname string, email string,
unique(lname, fname) ON CONFLICT REPLACE);

This will allow me to add multiple rows with the same lname as long as
the fnames are different for each.  Is there a way to define such unique
constraints in SqlAlchemy?
You need to put your UniqueConstraint in the __table_args__ class
attribute for SQLAlchemy to see it, something like this:

class User(Base):
     __tablename__ = 'user'
     __table_args__ = (
         UniqueConstraint('lname', 'fname', name='full_name'),
     )

See the examples at
http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/declarative.html#table-configuration.
Note that __table_args__ here is a 1-element tuple - the comma on the
end of the line is important.

Hope that helps,

Simon


Simon,

Thanks so much, that fixed it.  I appreciate the links too, I was
following the below and it hadn't occurred to me that it was a different
situation (i.e. defining the table using MetaData rather than using the
class definition).

http://docs.sqlalchemy.org/en/rel_0_8/core/schema.html?highlight=uniqueconstraint#sqlalchemy.schema.UniqueConstraint

thomas

--
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 http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to