[sqlalchemy] Re: declarative_base vs mapper

2009-07-29 Thread Mike Conley
Here is one approach. I would like to see some way of embedding it in
__table_args__, but I haven't been able to figure out that one.

t_xyz = xyz.__table__
indx = Index('myindex', t_xyz.c.type, t_xyz.c.name, unique=True)
indx.create()

or if you put the first 2 lines before metadata.create_all(), then you don't
need to separately create the index.

-- 
Mike Conley



On Wed, Jul 29, 2009 at 5:34 PM, Lukasz Szybalski szybal...@gmail.comwrote:


 Hello,
 How can I do
 Index('myindex', xyz.c.type, xyz.c.name, unique=True)

 in a declarative way?

 class xyz(DeclarativeBase):
__tablename__ = 'xyz'

#{ Columns

type = Column(Unicode(), nullable=False)
name = Column(Unicode(), nullable=False)


 how do I do index declarative style?

 Thanks,
 Lucas



 --
 Using rsync. How to setup rsyncd.
 http://lucasmanual.com/mywiki/rsync
 OpenLdap - From start to finish.
 http://lucasmanual.com/mywiki/OpenLdap

 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: declarative_base vs mapper

2009-07-29 Thread Lukasz Szybalski

On Wed, Jul 29, 2009 at 4:34 PM, Lukasz Szybalskiszybal...@gmail.com wrote:
 Hello,
 How can I do
 Index('myindex', xyz.c.type, xyz.c.name, unique=True)

 in a declarative way?

 class xyz(DeclarativeBase):
    __tablename__ = 'xyz'

    #{ Columns

    type = Column(Unicode(), nullable=False)
    name = Column(Unicode(), nullable=False)

__table_args__ = (ForeignKeyConstraint(...),UniqueConstraint(...),{})

Actually I needed a primary key on both fields.
 type = Column(Unicode(), nullable=False, primary_key=True)
 name = Column(Unicode(), nullable=False,primary_key=True , name=PK_xyz)

Now I wonder if name will name my primary key?
Do I specify auto_increment=No ?

What are the advantages of using declarative way of setting table
definitions? vs
addressbook_table = sqlalchemy.Table(Addressbook, metadata,
sqlalchemy.Column('Address_Sid', sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column('FirstName', sqlalchemy.Unicode(40),nullable=False),


class Addressbook(object):
def __init__(self, **kw):
automatically mapping attributes
for key, value in kw.iteritems():
setattr(self, key, value)

mapper(Addressbook, addressbook_table)



It seems to me as its harder to find information on proper syntax then
it is with regular table, py object, mapper?

Lucas








 how do I do index declarative style?

 Thanks,
 Lucas



 --
 Using rsync. How to setup rsyncd.
 http://lucasmanual.com/mywiki/rsync
 OpenLdap - From start to finish.
 http://lucasmanual.com/mywiki/OpenLdap




-- 
Using rsync. How to setup rsyncd.
http://lucasmanual.com/mywiki/rsync
OpenLdap - From start to finish.
http://lucasmanual.com/mywiki/OpenLdap

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: declarative_base vs mapper

2009-07-29 Thread Mike Conley
 What are the advantages of using declarative way of setting table
 definitions? vs
 addressbook_table = sqlalchemy.Table(Addressbook, metadata,
sqlalchemy.Column('Address_Sid', sqlalchemy.Integer, primary_key=True),
sqlalchemy.Column('FirstName', sqlalchemy.Unicode(40),nullable=False),
 

 class Addressbook(object):
def __init__(self, **kw):
automatically mapping attributes
for key, value in kw.iteritems():
setattr(self, key, value)

 mapper(Addressbook, addressbook_table)



 It seems to me as its harder to find information on proper syntax then
 it is with regular table, py object, mapper?

 Lucas


The declarative is a more concise all-in-one style

class Addressbook(Base):
__tablename__ = Addressbook
Address_Sid = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
FirstName = sqlalchemy.Column(sqlalchemy.Unicode(40),nullable=False)

accomplishes the same thing as the table + class + mapper example including
a default __init__() that accepts keywords for column values.

I do agree that the examples for declarative need to be more comprehensive.
I find it sometimes difficult to translate the mapper style into
declarative, but I'm getting better at it.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---