Re: [sqlalchemy] Using a arbitrary select mapper/class in a relation - is this allowed?

2010-02-04 Thread werner

On 03/02/2010 20:25, Michael Bayer wrote:

werner wrote:
   

In my model I have:

class Country(BaseExt):
  pass

sao.mapper(Country, createSelect(Country_D, Country_T, 'countries_d_id',
['name', 'url']))

Which I can use like this:

for cs in session.query(db.Country).all():
  print cs.name, cs.id

But I run into problems when I try to use Country in a relation like
this:

class Region_D(Base, CreateUpdateMixin):
  __tablename__ = u'regions_d'

  id = sa.Column(sa.Integer(), sa.Sequence('regions_d_id'),
primary_key=True, nullable=False)
  name = sa.Column(sa.String(length=50, convert_unicode=False))

  countries_d_id = sa.Column(sa.Integer())

  country = sao.relation('Country', backref='region_d',
primaryjoin='Region_D.countries_d_id == Country.id')

I am getting this exception also Country is defined before Region_D:
 

if you use 'Country' as a string in relation(), the declarative base
looks for it inside of Base._decl_class_registry.  Its not here since
Country isn't part of Base.   You should be saying Country, i.e. send
the actual class, to the relation().
   
Thanks, just tried this but I get the same exception.  Then changed 
Country to use Base and but then I get this exception.


Traceback (most recent call last):
  File saTest.py, line 5, in module
import model as db
  File D:\Dev\aaTests\sqla\i18nAlt2\model.py, line 223, in module
class Country(Base):
  File D:\Dev\aaTests\sqla\i18nAlt2\model.py, line 105, in __init__
return DeclarativeMeta.__init__(cls, classname, bases, dict_)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\ext\declarative.py, 
line 561, in __init__

_as_declarative(cls, classname, dict_)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.5.8-py2.5.egg\sqlalchemy\ext\declarative.py, 
line 516, in _as_declarative
specified and does not inherit from an existing table-mapped 
class. % cls)
sqlalchemy.exc.InvalidRequestError: Class class 'model.Country' does 
not have a __table__ or __tablename__ specified and does not inherit 
from an existing table-mapped class.


I am trying a property approach and that seems to do the trick.

I.e.:
class Region_D(Base, CreateUpdateMixin):
__tablename__ = u'regions_d'

id = sa.Column(sa.Integer(), sa.Sequence('regions_d_id'), 
primary_key=True, nullable=False)

name = sa.Column(sa.String(length=50, convert_unicode=False))

countries_d_id = sa.Column(sa.Integer(), 
sa.ForeignKey(u'countries_d.id'))


country_d = sao.relation('Country_D', backref='region_d')

country = TranslationPropertyAlt('Country', 'countries_d_id', 'id')

class TranslationPropertyAlt(object):
def __init__(self, tTable, fkCol):
self.tTable = tTable
self.fkCol = fkCol

def __get__(self, obj, objtype):
tTable = globals()[self.tTable]
fKey = getattr(obj, self.fkCol)
try:
return 
sao.object_session(obj).query(tTable).filter_by(id=fKey).one()

except sao.exc.NoResultFound:
print 'no translation was found'
print 'tTable: %s' % (self.tTable)
traceback.print_exc()

Need to do more testing and clean up the code a lot - but I think there 
is a very small light at the end of the tunnel.


Thanks again for all the tips and help
Werner


--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



RE: [sqlalchemy] Using a arbitrary select mapper/class in a relation - is this allowed?

2010-02-04 Thread King Simon-NFHD78
 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:sqlalch...@googlegroups.com] On Behalf Of werner
 Sent: 04 February 2010 09:41
 To: sqlalchemy@googlegroups.com
 Subject: Re: [sqlalchemy] Using a arbitrary select 
 mapper/class in a relation - is this allowed?
 
 On 03/02/2010 20:25, Michael Bayer wrote:
  werner wrote:
 
  In my model I have:
 
  class Country(BaseExt):
pass
 
  sao.mapper(Country, createSelect(Country_D, Country_T, 
 'countries_d_id',
  ['name', 'url']))
 
  Which I can use like this:
 
  for cs in session.query(db.Country).all():
print cs.name, cs.id
 
  But I run into problems when I try to use Country in a 
 relation like
  this:
 
  class Region_D(Base, CreateUpdateMixin):
__tablename__ = u'regions_d'
 
id = sa.Column(sa.Integer(), sa.Sequence('regions_d_id'),
  primary_key=True, nullable=False)
name = sa.Column(sa.String(length=50, convert_unicode=False))
 
countries_d_id = sa.Column(sa.Integer())
 
country = sao.relation('Country', backref='region_d',
  primaryjoin='Region_D.countries_d_id == Country.id')
 
  I am getting this exception also Country is defined 
 before Region_D:
   
  if you use 'Country' as a string in relation(), the 
 declarative base
  looks for it inside of Base._decl_class_registry.  Its not 
 here since
  Country isn't part of Base.   You should be saying 
 Country, i.e. send
  the actual class, to the relation().
 
 Thanks, just tried this but I get the same exception. 


Just to confirm, were you actually defining your Region_D class exactly
like this:


class Region_D(Base, CreateUpdateMixin):
__tablename__ = u'regions_d'
id = sa.Column(sa.Integer(), sa.Sequence('regions_d_id'),
   primary_key=True, nullable=False)

name = sa.Column(sa.String(length=50, convert_unicode=False))

countries_d_id = sa.Column(sa.Integer())

country = sao.relation(Country, backref='region_d',
   primaryjoin=countries_d_id == Country.id)

Ie. Neither the class nor the primaryjoin parameters in the relation
were strings? I would be surprised if you got the exception you
described (expression 'Country' failed to locate a name ) if you had
done that, because SA wouldn't be trying to look up that name.

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



RE: [sqlalchemy] Another tutorial!

2010-02-04 Thread King Simon-NFHD78
 -Original Message-
 From: sqlalchemy@googlegroups.com 
 [mailto:sqlalch...@googlegroups.com] On Behalf Of Mike Driscoll
 Sent: 04 February 2010 03:34
 To: sqlalchemy
 Subject: [sqlalchemy] Another tutorial!
 
 Hi,
 
 I just finished up a tutorial series on SqlAlchemy that I thought I'd
 share:
 
 http://www.blog.pythonlibrary.org/2010/02/03/another-step-by-s
 tep-sqlalchemy-tutorial-part-1-of-2/
 http://www.blog.pythonlibrary.org/2010/02/03/another-step-by-s
 tep-sqlalchemy-tutorial-part-2-of-2/
 
 Hopefully it's made well enough that people can follow the tutorial
 easily. Let me know if I made any serious blunders.
 
 Thanks,
 
 Mike
 

Hi Mike,

Not a serious blunder, but I think there may be a small mistake in part
2, where you describe updating an email address:

  # change the first address
  prof.addresses[0] = Address(pr...@marvel.com)

I don't think this is going to update the 'pr...@dc.com' row in the
database to say 'pr...@marvel.com'. Instead, it is going to disconnect
that row from the user by setting the user_id to NULL, and add a new row
with the new address. (This may be what you intended, but I don't think
it's clear from the description).

I would have thought that you'd actually want to write this:

  # change the first address
  prof.addresses[0].email_address = pr...@marvel.com

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] Using a arbitrary select mapper/class in a relation - is this allowed?

2010-02-04 Thread werner

On 04/02/2010 11:07, King Simon-NFHD78 wrote:

...

Just to confirm, were you actually defining your Region_D class exactly
like this:


class Region_D(Base, CreateUpdateMixin):
 __tablename__ = u'regions_d'
 id = sa.Column(sa.Integer(), sa.Sequence('regions_d_id'),
primary_key=True, nullable=False)

 name = sa.Column(sa.String(length=50, convert_unicode=False))

 countries_d_id = sa.Column(sa.Integer())

 country = sao.relation(Country, backref='region_d',
primaryjoin=countries_d_id == Country.id)

Ie. Neither the class nor the primaryjoin parameters in the relation
were strings? I would be surprised if you got the exception you
described (expression 'Country' failed to locate a name ) if you had
done that, because SA wouldn't be trying to look up that name.
   

Oops, I had left the primaryjoin as a string.

Simon, thanks for catching this.

There is now a much bigger light at the end of the tunnel.

Werner

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] 0.6b1 and pymssql

2010-02-04 Thread Michael Bayer

On Feb 4, 2010, at 1:13 AM, Domingo Aguilera wrote:

 I am trying to use 0.6b1 with pymssql ( latest version 1.0.2 ).  For
 it I use create_engine with an uri like mssql+pymssql://
 
 It seems 0.6b1 looks for a dbapi attribute that is not present in
 pymssql.  Is there a workaround for this?


theres two things going on there.   one is that pymssql has been rewritten 
recently.   well, thats actually pretty much it - we havent done any testing 
with pymssql.If you can provide details we can commit a patch.   In 0.6 we 
can probably just shoot for supporting the new pymssql since its supposed to 
be much better than the old.




 
 Tks.
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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: 0.6b1 and pymssql

2010-02-04 Thread Domingo Aguilera
What is the version of pymssql that worked with 0.6 ?


On Feb 4, 7:12 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On Feb 4, 2010, at 1:13 AM, Domingo Aguilera wrote:

  I am trying to use 0.6b1 with pymssql ( latest version 1.0.2 ).  For
  it I use create_engine with an uri like mssql+pymssql://

  It seems 0.6b1 looks for a dbapi attribute that is not present in
  pymssql.  Is there a workaround for this?

 theres two things going on there.   one is that pymssql has been rewritten 
 recently.   well, thats actually pretty much it - we havent done any testing 
 with pymssql.    If you can provide details we can commit a patch.   In 0.6 
 we can probably just shoot for supporting the new pymssql since its 
 supposed to be much better than the old.





  Tks.

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

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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: Another tutorial!

2010-02-04 Thread Mike Driscoll


On Feb 4, 4:24 am, King Simon-NFHD78 simon.k...@motorola.com
wrote:
  -Original Message-
  From: sqlalchemy@googlegroups.com
  [mailto:sqlalch...@googlegroups.com] On Behalf Of Mike Driscoll
  Sent: 04 February 2010 03:34
  To: sqlalchemy
  Subject: [sqlalchemy] Another tutorial!

  Hi,

  I just finished up a tutorial series on SqlAlchemy that I thought I'd
  share:

 http://www.blog.pythonlibrary.org/2010/02/03/another-step-by-s
  tep-sqlalchemy-tutorial-part-1-of-2/
 http://www.blog.pythonlibrary.org/2010/02/03/another-step-by-s
  tep-sqlalchemy-tutorial-part-2-of-2/

  Hopefully it's made well enough that people can follow the tutorial
  easily. Let me know if I made any serious blunders.

  Thanks,

  Mike

 Hi Mike,

 Not a serious blunder, but I think there may be a small mistake in part
 2, where you describe updating an email address:

   # change the first address
   prof.addresses[0] = Address(pr...@marvel.com)

 I don't think this is going to update the 'pr...@dc.com' row in the
 database to say 'pr...@marvel.com'. Instead, it is going to disconnect
 that row from the user by setting the user_id to NULL, and add a new row
 with the new address. (This may be what you intended, but I don't think
 it's clear from the description).

 I would have thought that you'd actually want to write this:

   # change the first address
   prof.addresses[0].email_address = pr...@marvel.com

 Hope that helps,

 Simon

I was testing this in IDLE and it seemed to work when I did
prof.addresses to check it. If the user gets set to NULL, wouldn't
prof.addresses only show one entry? I'll check it out and make sure.
If I messed it up, I'll get it fixed. Thanks for the bug report!

- Mike

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] order_by() and count()

2010-02-04 Thread gsandorx
hi,
i have a typical one to many relationship between two tables:

Table_A: contains, for example, Stores (id, store_name)
Table_B: contains products, Prod,  and which store the products
belong to: (id, name, store_id)

I need to create a query where i get all Store objects ordered by
the number of products, e.g:

store_3   34 (amount of products)
store_1   23 (ditto)
store_2   18 (ditto)

any idea?

cheers,
sandor

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] Re: 0.6b1 and pymssql

2010-02-04 Thread Michael Bayer
Domingo Aguilera wrote:
 What is the version of pymssql that worked with 0.6 ?

judging by what I see here, http://pymssql.sourceforge.net/ and
http://sourceforge.net/projects/pymssql/files/, its anything from the
pre-1.0 rewrite.  But that's also from the 0.3/0.4 days.   note the most
recent version is from 2006.   None of them have been tested with 0.6 to
my knowledge but there is also very little that is pymssql-specific in the
current codebase, so getting 1.0 to work is very possibly a matter of
turning the correct options on and off on the dialect.




 On Feb 4, 7:12 am, Michael Bayer mike...@zzzcomputing.com wrote:
 On Feb 4, 2010, at 1:13 AM, Domingo Aguilera wrote:

  I am trying to use 0.6b1 with pymssql ( latest version 1.0.2 ).  For
  it I use create_engine with an uri like mssql+pymssql://

  It seems 0.6b1 looks for a dbapi attribute that is not present in
  pymssql.  Is there a workaround for this?

 theres two things going on there.   one is that pymssql has been
 rewritten recently.   well, thats actually pretty much it - we havent
 done any testing with pymssql.    If you can provide details we can
 commit a patch.   In 0.6 we can probably just shoot for supporting the
 new pymssql since its supposed to be much better than the old.





  Tks.

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

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] innerjoin parameter missing in orm.properties.RelationProperty

2010-02-04 Thread Michael Bayer
Julien Cigar wrote:
 Hello,

 I'm using SQLAlchemy 0.5.8 with the following mapper definition :
 http://www.pastie.org/809364

 According to the documentation
 (http://www.sqlalchemy.org/docs/reference/orm/mapping.html#sqlalchemy.orm.relation)
 I can use an innerjoin parameter on a relation() (to generate an INNER
 JOIN instead of a LEFT OUTER JOIN), but this parameter seems not to
 exist in orm.properties.RelationProperty class, I get the following error
 :

 File
 /home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/orm/__init__.py,
 line 423, in relation
  return RelationProperty(argument, secondary=secondary, **kwargs)
 TypeError: __init__() got an unexpected keyword argument 'innerjoin'

 Is it a bug or am I doing something wrong ?

 Thanks,
 Julien

 (... and congratulations for 0.6 !)


hey ho, I guess we're going to get a lot of these for awhile.   The docs
default to 0.6 now.   The 0.5 docs are at
http://www.sqlalchemy.org/docs/05/ .



 --
 No trees were killed in the creation of this message.
 However, many electrons were terribly inconvenienced.

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



RE: [sqlalchemy] Re: Another tutorial!

2010-02-04 Thread King Simon-NFHD78
Mike Driscoll wrote:
 
 On Feb 4, 8:30 am, Mike Driscoll kyoso...@gmail.com wrote:
  On Feb 4, 4:24 am, King Simon-NFHD78 simon.k...@motorola.com
  wrote:
 

[SNIP]

 
   Not a serious blunder, but I think there may be a small 
 mistake in part
   2, where you describe updating an email address:
 
     # change the first address
     prof.addresses[0] = Address(pr...@marvel.com)
 
   I don't think this is going to update the 'pr...@dc.com' 
 row in the
   database to say 'pr...@marvel.com'. Instead, it is going 
 to disconnect
   that row from the user by setting the user_id to NULL, 
 and add a new row
   with the new address. (This may be what you intended, but 
 I don't think
   it's clear from the description).
 
   I would have thought that you'd actually want to write this:
 
     # change the first address
     prof.addresses[0].email_address = pr...@marvel.com
 
   Hope that helps,
 
   Simon
 
  I was testing this in IDLE and it seemed to work when I did
  prof.addresses to check it. If the user gets set to NULL, wouldn't
  prof.addresses only show one entry? I'll check it out and make sure.
  If I messed it up, I'll get it fixed. Thanks for the bug report!
 
  - Mike
 
 I just ran through that section again using the Python interpreter and
 after changing the address like this:
 
 prof.addresses[0] = Address(pr...@marvel.com)
 
 I then used the following (per the official tutorial):
 
  prof.addresses[0].user
 User('Prof','Prof. Xavier', 'fudge')
 
 So my method appears to work. I tried your method too:
 
  prof.addresses[0].email_address = prof...@image.com
  prof.addresses[0].user
 User('Prof','Prof. Xavier', 'fudge')
 
 
 That appears to give the same result. Let me know if I am
 misunderstanding something basic here.
 

The difference is that in your case, there is now a row in the Address table 
without an associated User. Try running the following:

for address in session.query(Address):
print Address %r belongs to User %r % (address, address.user)


I think you will see addresses that don't belong to any users.

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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: Another tutorial!

2010-02-04 Thread Mike Driscoll


On Feb 4, 9:26 am, King Simon-NFHD78 simon.k...@motorola.com
wrote:
 Mike Driscoll wrote:

  On Feb 4, 8:30 am, Mike Driscoll kyoso...@gmail.com wrote:
   On Feb 4, 4:24 am, King Simon-NFHD78 simon.k...@motorola.com
   wrote:

 [SNIP]





Not a serious blunder, but I think there may be a small
  mistake in part
2, where you describe updating an email address:

  # change the first address
  prof.addresses[0] = Address(pr...@marvel.com)

I don't think this is going to update the 'pr...@dc.com'
  row in the
database to say 'pr...@marvel.com'. Instead, it is going
  to disconnect
that row from the user by setting the user_id to NULL,
  and add a new row
with the new address. (This may be what you intended, but
  I don't think
it's clear from the description).

I would have thought that you'd actually want to write this:

  # change the first address
  prof.addresses[0].email_address = pr...@marvel.com

Hope that helps,

Simon

   I was testing this in IDLE and it seemed to work when I did
   prof.addresses to check it. If the user gets set to NULL, wouldn't
   prof.addresses only show one entry? I'll check it out and make sure.
   If I messed it up, I'll get it fixed. Thanks for the bug report!

   - Mike

  I just ran through that section again using the Python interpreter and
  after changing the address like this:

  prof.addresses[0] = Address(pr...@marvel.com)

  I then used the following (per the official tutorial):

   prof.addresses[0].user
  User('Prof','Prof. Xavier', 'fudge')

  So my method appears to work. I tried your method too:

   prof.addresses[0].email_address = prof...@image.com
   prof.addresses[0].user
  User('Prof','Prof. Xavier', 'fudge')

  That appears to give the same result. Let me know if I am
  misunderstanding something basic here.

 The difference is that in your case, there is now a row in the Address table 
 without an associated User. Try running the following:

 for address in session.query(Address):
     print Address %r belongs to User %r % (address, address.user)

 I think you will see addresses that don't belong to any users.

 Simon

Thanks Simon! That made sense. I've fixed my example to match what you
said. Sorry about that.

- Mike

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



RE: [sqlalchemy] Re: Another tutorial!

2010-02-04 Thread King Simon-NFHD78
Mike Driscoll wrote:
 
 Thanks Simon! That made sense. I've fixed my example to match what you
 said. Sorry about that.
 
 - Mike
 

No problem. I'm afraid you still have a typo though. You have:

  addresses[0].email_address = Address(pr...@marvel.com)

Whereas you want:

  addresses[0].email_address = pr...@marvel.com

:-)

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



RE: [sqlalchemy] Another tutorial!

2010-02-04 Thread John Trammell
You made some serious blunders.  Check your comments on the blog post.

 -Original Message-
 From: sqlalchemy@googlegroups.com [mailto:sqlalch...@googlegroups.com]
 On Behalf Of Mike Driscoll
 Sent: Wednesday, February 03, 2010 9:34 PM
 To: sqlalchemy
 Subject: [sqlalchemy] Another tutorial!
 
 Hi,
 
 I just finished up a tutorial series on SqlAlchemy that I thought I'd
 share:
 
 http://www.blog.pythonlibrary.org/2010/02/03/another-step-by-step-
 sqlalchemy-tutorial-part-1-of-2/
 http://www.blog.pythonlibrary.org/2010/02/03/another-step-by-step-
 sqlalchemy-tutorial-part-2-of-2/
 
 Hopefully it's made well enough that people can follow the tutorial
 easily. Let me know if I made any serious blunders.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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: Another tutorial!

2010-02-04 Thread Mike Driscoll
Simon,

On Feb 4, 10:15 am, King Simon-NFHD78 simon.k...@motorola.com
wrote:
 Mike Driscoll wrote:

  Thanks Simon! That made sense. I've fixed my example to match what you
  said. Sorry about that.

  - Mike

 No problem. I'm afraid you still have a typo though. You have:

   addresses[0].email_address = Address(pr...@marvel.com)

 Whereas you want:

   addresses[0].email_address = pr...@marvel.com

 :-)

 Simon

Oops...you're right. I must be blind today. Fixed (again)!

---
Mike Driscoll

Blog:   http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21  http://us.pycon.org/

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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: Another tutorial!

2010-02-04 Thread Mike Driscoll


On Feb 4, 10:36 am, John Trammell jo...@holmescorp.com wrote:
 You made some serious blunders.  Check your comments on the blog post.


It looks like Werner found the same issue that Simon already told me
about. This has been fixed per Simon's notes. I also found that I
forgot to import ForeignKey in the first part of the series. This was
also fixed. Thanks for being so nice.

---
Mike Driscoll

Blog:   http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21  http://us.pycon.org/

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] 0.6b1 - meta.create_all with FB

2010-02-04 Thread werner

I am doing a bit of testing and run into this exception.

Traceback (most recent call last):
  File saCreateDb.py, line 31, in module
meta.create_all(engine)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\schema.py, 
line 1934, in create_all

bind.create(self, checkfirst=checkfirst, tables=tables)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\base.py, 
line 1393, in create
self._run_visitor(ddl.SchemaGenerator, entity, 
connection=connection, **kwargs)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\base.py, 
line 1424, in _run_visitor

visitorcallable(self.dialect, conn, **kwargs).traverse(element)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\sql\visitors.py, 
line 86, in traverse

return traverse(obj, self.__traverse_options__, self._visitor_dict)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\sql\visitors.py, 
line 197, in traverse

return traverse_using(iterate(obj, opts), obj, visitors)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\sql\visitors.py, 
line 191, in traverse_using

meth(target)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\ddl.py, 
line 42, in visit_metadata

self.traverse_single(table)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\sql\visitors.py, 
line 76, in traverse_single

return meth(obj)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\ddl.py, 
line 55, in visit_table

self.connection.execute(schema.CreateTable(table))
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\base.py, 
line 1035, in execute

return Connection.executors[c](self, object, multiparams, params)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\base.py, 
line 1084, in _execute_ddl

return self.__execute_context(context)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\base.py, 
line 1120, in __execute_context
self._cursor_execute(context.cursor, context.statement, 
context.parameters[0], context=context)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\base.py, 
line 1182, in _cursor_execute

self._handle_dbapi_exception(e, statement, parameters, cursor, context)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\engine\base.py, 
line 1180, in _cursor_execute

self.dialect.do_execute(cursor, statement, parameters, context=context)
  File 
c:\python25\lib\site-packages\sqlalchemy-0.6beta1-py2.5.egg\sqlalchemy\dialects\firebird\base.py, 
line 614, in do_execute

cursor.execute(statement, parameters or [])
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (-104, 
'isc_dsql_prepare: \n  Dynamic SQL Error\n  SQL error code = -104\n  
Token unknown - line 4, column 14\n  ,') '\nCREATE TABLE users (\n\tid 
INTEGER NOT NULL, \n\tname VARCHAR, \n\tfullname VARCHAR, \n\tpassword 
VARCHAR, \n\tPRIMARY KEY (id)\n)\n\n' ()


Same code using 0.5.8 works fine.

The model for users is:

class User(Base):

__tablename__ = users

id = sa.Column(sa.Integer, sa.Sequence('users_id'), primary_key=True)
name = sa.Column(sa.String)
fullname = sa.Column(sa.String)
password = sa.Column(sa.String)

It looks (using echo=True) like 0.5.8 used BLOB instead of VARCHAR for 
String without a size.


I guess this is as designed looking at 06Migration - Miscellaneous API 
changes.


If this as designed then maybe the tutorials should change from e.g.:

name = Column(String)
to
name = Column(String, length=30)

Werner

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] 0.6b1 - meta.create_all with FB

2010-02-04 Thread Michael Bayer
werner wrote:
 I am doing a bit of testing and run into this exception.
 line 614, in do_execute
  cursor.execute(statement, parameters or [])
 sqlalchemy.exc.ProgrammingError: (ProgrammingError) (-104,
 'isc_dsql_prepare: \n  Dynamic SQL Error\n  SQL error code = -104\n
 Token unknown - line 4, column 14\n  ,') '\nCREATE TABLE users (\n\tid
 INTEGER NOT NULL, \n\tname VARCHAR, \n\tfullname VARCHAR, \n\tpassword
 VARCHAR, \n\tPRIMARY KEY (id)\n)\n\n' ()

 Same code using 0.5.8 works fine.

 The model for users is:

 class User(Base):
  
  __tablename__ = users

  id = sa.Column(sa.Integer, sa.Sequence('users_id'), primary_key=True)
  name = sa.Column(sa.String)
  fullname = sa.Column(sa.String)
  password = sa.Column(sa.String)

 It looks (using echo=True) like 0.5.8 used BLOB instead of VARCHAR for
 String without a size.

that sounds very strange.  0.4 and earlier would use TEXT for String with
no size.  In 0.5 we did away with that, as its a surprise - we had lot of
string with no length complaints, but I would prefer people learn about
String/String(50) instead of assuming that String always needs a length,
since that adds to the verbose narrative which really isn't true.  You
need the length only if you are issuing CREATE TABLE and you are on a
database that cares about the length.  Otherwise you do not need it (PG
and SQLite both allow VARCHAR with no length).   String is used in other
places besides table metadata, including func.foo(..., type_=String),
cast(x, String), etc.

I'm not aware off the top of my head which dialect could have been
emitting BLOB for VARCHAR, since those two types aren't even compatible.


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] 0.6b1 - meta.create_all with FB

2010-02-04 Thread werner

Michael,

Following is just a suggestion.
...

that sounds very strange.  0.4 and earlier would use TEXT for String with
no size.  In 0.5 we did away with that, as its a surprise - we had lot of
string with no length complaints, but I would prefer people learn about
String/String(50) instead of assuming that String always needs a length,
since that adds to the verbose narrative which really isn't true.
When I started to use SA I also thought one had to be very verbose until 
you showed me some tricks.


If verbose is still something hanging on SA I would suggest to 
change the tutorials, e.g. to something like this:


Most tutorial/sample code for declarative show something like this:

class User(Base):

__tablename__ = users

id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)

#--
def __init__(self, name, fullname, password):
Constructor
self.name = name
self.fullname = fullname
self.password = password

def __repr__(self):
return User('%s','%s', '%s') % (self.name, self.fullname, 
self.password)


Much shorter would be the following and I believe it would work with all 
databases.


class User(Base):

__tablename__ = users

id = Column(Integer, Sequence('users_id'), primary_key=True)
name = Column(String(30))
fullname = Column(String(30))
password = Column(String(20))

Then in the text explain that sequence and (30) is only needed with 
certain db's.


Use the above type of definition with the following:

class BaseExt(object):
Does much nicer repr/print of class instances
from sqlalchemy list suggested by Michael Bayer

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 = declarative_base(cls=BaseExt)

Werner

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] 0.6b1 - meta.create_all with FB

2010-02-04 Thread Michael Bayer
werner wrote:
 Michael,

 Following is just a suggestion.
 ...
 that sounds very strange.  0.4 and earlier would use TEXT for String
 with
 no size.  In 0.5 we did away with that, as its a surprise - we had lot
 of
 string with no length complaints, but I would prefer people learn
 about
 String/String(50) instead of assuming that String always needs a length,
 since that adds to the verbose narrative which really isn't true.
 When I started to use SA I also thought one had to be very verbose until
 you showed me some tricks.

 If verbose is still something hanging on SA I would suggest to
 change the tutorials, e.g. to something like this:

 Most tutorial/sample code for declarative show something like this:

OK i see the point about the constructor, though I want to get across that
yes the constructor is optional with declarative, but you can still make
one.   that might be too much to ask.   as far as Sequence and such, I
still would rather leave those out of the examples - since we're on Sphinx
I should learn to put a little sidebox type of thing that lays out those
issues straight in the tutorial when the mapping is first introduced, i.e.
with mysql, fb, oracle, etc. you need to consider XYZ.



 class User(Base):
  
  __tablename__ = users

  id = Column(Integer, primary_key=True)
  name = Column(String)
  fullname = Column(String)
  password = Column(String)

  #--
  def __init__(self, name, fullname, password):
  Constructor
  self.name = name
  self.fullname = fullname
  self.password = password

  def __repr__(self):
  return User('%s','%s', '%s') % (self.name, self.fullname,
 self.password)

 Much shorter would be the following and I believe it would work with all
 databases.

 class User(Base):
  
  __tablename__ = users

  id = Column(Integer, Sequence('users_id'), primary_key=True)
  name = Column(String(30))
  fullname = Column(String(30))
  password = Column(String(20))

 Then in the text explain that sequence and (30) is only needed with
 certain db's.

 Use the above type of definition with the following:

 class BaseExt(object):
  Does much nicer repr/print of class instances
  from sqlalchemy list suggested by Michael Bayer
  
  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 = declarative_base(cls=BaseExt)

 Werner

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] 0.6b1 and pymssql

2010-02-04 Thread Yannick Gingras
On February 4, 2010, Michael Bayer wrote:
 In 0.6 we can probably just shoot for supporting the new pymssql
 since its supposed to be much better than the old.

I've seen pymssql 1.0.2 segfault the interpreter when under medium
load.  And yes, I had a core to prove that pymssql was as fault.  I
would probably consider pyodbc is I was stuck with MSSQL.

-- 
Yannick Gingras
http://ygingras.net
http://confoo.ca -- track coordinator
http://montrealpython.org -- lead organizer


signature.asc
Description: This is a digitally signed message part.


Re: [sqlalchemy] 0.6b1 - meta.create_all with FB

2010-02-04 Thread Michael Bayer

On Feb 4, 2010, at 3:39 PM, werner wrote:

 
 OK i see the point about the constructor, though I want to get across that
 yes the constructor is optional with declarative, but you can still make
 one.
 Could the constructor be done in a similar way as you suggested to do the 
 __repr__?

it certainly can, but I'm really trying to make the tutorial look like regular 
Python objects like any Python beginner is familiar with.  you'd be surprised 
how thrown off people get even by a simple for k in kwargs: setattr(k, 
kwargs[k]) type of thing.

that might be too much to ask.   as far as Sequence and such, I
 still would rather leave those out of the examples - since we're on Sphinx
 I should learn to put a little sidebox type of thing that lays out those
 issues straight in the tutorial when the mapping is first introduced, i.e.
 with mysql, fb, oracle, etc. you need to consider XYZ.
   
 Sidebox?  You mean rst admonitions?
 http://docutils.sourceforge.net/docs/ref/rst/directives.html#admonitions

yes !


 
 E.g. something like this :
 
 .. tip::
With FB, . etc you should use Sequence ... for your primary key.
 
 .. tip::
With FB,  you need/have to define the length of your
 
 Werner
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] 0.6b1 and pymssql

2010-02-04 Thread Michael Bayer

On Feb 4, 2010, at 5:49 PM, Yannick Gingras wrote:

 On February 4, 2010, Michael Bayer wrote:
 In 0.6 we can probably just shoot for supporting the new pymssql
 since its supposed to be much better than the old.
 
 I've seen pymssql 1.0.2 segfault the interpreter when under medium
 load.  And yes, I had a core to prove that pymssql was as fault.  I
 would probably consider pyodbc is I was stuck with MSSQL.

did you contact the pymssql maintainer about that ?   Also, even if pymssql was 
at fault I've observed that sometimes native libs segfault due to specific 
usages, which the dialect can be made to ensure never occur.


 
 -- 
 Yannick Gingras
 http://ygingras.net
 http://confoo.ca -- track coordinator
 http://montrealpython.org -- lead organizer

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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] mapper class with multiple tables

2010-02-04 Thread atomekk
Hi

I'm having trouble with define mapper for class which will include
data from more than 2 tables (I'm using Formalchemy for form
generation).

Quick brieffing on my model:

# account table
acc_account_table = sa.Table('acc_account', meta.metadata,
sa.Column('account_id', sa.types.Integer, primary_key=True),
...

# address_book table
acc_address_book_table = sa.Table('acc_address_book', meta.metadata,
sa.Column('address_book_id', sa.types.Integer, primary_key=True),
sa.Column('account_id', sa.types.Integer,
  sa.ForeignKey('acc_account.account_id'),
nullable=False),
...

# user table
acc_user_table = sa.Table('acc_user', meta.metadata,
sa.Column('user_id', sa.types.Integer, primary_key=True),
sa.Column('account_id', sa.types.Integer,
  sa.ForeignKey('acc_account.account_id')),
...

When i try mapper with 2 table join like:

orm.mapper(AccAccountUser, orm.join(acc_account_table,
acc_user_table),
properties={ 'account_id': [
 
acc_account_table.c.account_id,
acc_user_table.c.account_id]
})

Then it work ok but i want also to include data from third table
address_book which has also foreign_key to account table so i definied
next mapper which should give me the data from those 3 tables for
Formalchemy:

orm.mapper(AccAccountUserAddress, orm.join(acc_account_table,
acc_user_table,
acc_address_book_table),
properties={ 'account_id': [
 
acc_account_table.c.account_id,
acc_user_table.c.account_id,
 
acc_address_book_table.c.account_id]
})

but it gives me error: sqlalchemy.exc.ArgumentError: Column
'acc_address_book.account_id' is not represented in mapper's table.

Maybe i'm doing something wrong with that second mapper join.

Thanks,

Thomas

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] 0.6b1 and pymssql

2010-02-04 Thread Yannick Gingras
On February 4, 2010, Michael Bayer wrote:
  I've seen pymssql 1.0.2 segfault the interpreter when under medium
  load.  And yes, I had a core to prove that pymssql was as fault.  I
  would probably consider pyodbc is I was stuck with MSSQL.

 did you contact the pymssql maintainer about that ?  Also, even if
 pymssql was at fault I've observed that sometimes native libs
 segfault due to specific usages, which the dialect can be made to
 ensure never occur.

If I recall correctly, we were still trying to come up with an easy to
reproduce test case for that one when the project got canned and we
moved to MySQL.  For the record, the segfault occurred in
PyTuple_SET_ITEM() on line 2186 of mssqldbmodule.c.  I don't have an
MSSQL instance handy right now so it's hard to give details.

-- 
Yannick Gingras
http://ygingras.net
http://confoo.ca -- track coordinator
http://montrealpython.org -- lead organizer


signature.asc
Description: This is a digitally signed message part.