Hi,

AF wrote:
> Am starting to experiment with declarative base.
>
> In the code block at:
>
> http://www.sqlalchemy.org/docs/05/ormtutorial.html#building-a-relation
>
> Why does __init__() not contain any mention of the foreign key
> "user_id"?
>
>   
As one normally does not manually set an id - it is done with a sequence 
in most cases.

IIRC correctly the __init__ section is only needed if you want to do:
add = Address('an email address')

I never do this, i.e. I assign like this
add = Address()
add.email_address = 'an email address'
> In what way is the Address object expected to be instantiated such
> that it receives the correct user id?
>   
You just do this and SA will take care of the id's:
con = db.Contact()
con.name = 'a name'

add = db.Address()
add.street = 'a street'

con.address.append(add)

session.add(con)
session.add(add)
session.commit()


Above is based on this model (db) and the BaseExt was a tip I had from 
Michael.

class BaseExt(object):
    def __repr__(self):
        return "%s(%s)" % (
                 (self.__class__.__name__),
                 ', '.join(["%s=%r" % (key, getattr(self, key))
                            for key in sorted(self.__dict__.keys())
                            if not key.startswith('_')]))

Base = sad.declarative_base(cls=BaseExt)
metadata = Base.metadata

class Contact(Base):
    __table__ = sa.Table(u'contact', metadata,
    sa.Column(u'id', sa.Integer(), sa.Sequence('gen_contact_id'), 
primary_key=True, nullable=False),
    sa.Column(u'name', sa.String(length=70, convert_unicode=False), 
nullable=False),
    )
   
class Address(Base):
    __table__ = sa.Table(u'address', metadata,
    sa.Column(u'id', sa.Integer(), sa.Sequence('gen_contact_id'), 
primary_key=True, nullable=False),
    sa.Column(u'street', sa.String(length=70, convert_unicode=False), 
nullable=False),
    sa.Column(u'fk_contact', sa.Integer(), sa.ForeignKey(u'contact.id'), 
nullable=False),
    )
   
    contact = sao.relation('Contact', backref='address')

Werner


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to