[sqlalchemy] Re: Relation w/ declarative

2009-08-06 Thread werner

Allen,

allen.fowler wrote:

...
>
> What is that __repr__ function doing exactly?  Looks interesting.
>   
It is really useful, and yes Michael comes up with beauties like this.

Produces nice print output, i.e.:
print 'after flush'
print con
print add

Will give you:
after flush
Contact(address=[Address(contact=Contact(address=[...], id=7, name='a 
name'), fk_contact=7, id=8, street='a street')], id=7, name='a name')
Address(contact=Contact(address=[Address(contact=Contact(address=[...], 
id=7, name='a name'), fk_contact=7, id=8, street='a street')], id=7, 
name='a name'), fk_contact=7, id=8, street='a street')

If it were not there you would get:
after flush



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



[sqlalchemy] Re: Relation w/ declarative

2009-08-06 Thread allen.fowler



On Aug 6, 4:59 am, Wichert Akkerman  wrote:
> On 8/6/09 09:30 , werner wrote:
>
> > 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'
>
> You can also do this with the default declarative base constructor:
>
>    add = Address(email_address='j...@example.com')
>
> All columns can be used as named parameters.
>

Brilliant! :)




--~--~-~--~~~---~--~~
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: Relation w/ declarative

2009-08-06 Thread allen.fowler



On Aug 6, 3:30 am, werner  wrote:

> 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()
>

OK, that explains it.  Thank you.



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

What is that __repr__ function doing exactly?  Looks interesting.

--~--~-~--~~~---~--~~
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: Relation w/ declarative

2009-08-06 Thread Wichert Akkerman

On 8/6/09 09:30 , werner wrote:
> 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'

You can also do this with the default declarative base constructor:

   add = Address(email_address='j...@example.com')

All columns can be used as named parameters.

Wichert.

--~--~-~--~~~---~--~~
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: Relation w/ declarative

2009-08-06 Thread werner

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