[sqlalchemy] array column as primary key

2010-01-13 Thread avdd
I want to map a table with a postgresql array as a primary key.
PostgreSQL supports it, and everything works until the session wants
to use the list returned from the query as an instance key.   How can
I intercept the row returned to wrap it in a tuple?  I can't figure
out translate_row!


from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.postgresql.base import ARRAY

engine = create_engine('postgresql:///avdd')
DB = sessionmaker(bind=engine)

class A(declarative_base()):
__tablename__ = 'a'
ids = Column(ARRAY(Integer()), primary_key=True)

A.__table__.delete(bind=engine).execute()

a = A()
a.ids = (1,2,3)

db = DB()
db.add(a)
db.commit()

del a, db

db = DB()
print db.query(A).all()


Traceback (most recent call last):
  File testarraypk.py, line 25, in module
print db.query(A).all()
  File lib/sqlalchemy/orm/query.py, line 1217, in all
return list(self)
  File lib/sqlalchemy/orm/query.py, line 1376, in instances
rows = [process[0](row, None) for row in fetch]
  File lib/sqlalchemy/orm/mapper.py, line 1681, in _instance
instance = session_identity_map.get(identitykey)
  File lib/sqlalchemy/orm/identity.py, line 145, in get
state = dict.get(self, key, default)
TypeError: unhashable type: 'list'
-- 
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] Hi,I found a codec bug,and I write a patch.

2010-01-13 Thread 诚子
detial please see

http://my.unix-center.net/~WeiZhicheng/2010/01/13/sqlalchemy-0-6-unicodedecodeerror-bug-patch/

-- 
my.unix-center.net/~WeiZhicheng
-- 

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] tracking back from logged sql back to the python that caused it

2010-01-13 Thread Chris Withers

Michael Bayer wrote:

Chris Withers wrote:

Easiest way to trap it is to monkeypatch Session.rollback(), or whatever
rollback you think you might be calling, with a def that calls
pdb.set_trace() or dumps a stack trace using inspect().

Any chance of SA growing the ability to do the latter itself?


Again, I really don't know what you mean here, unless you're asking for a
enable_pdb=True keyword on rollback() or something, which I doubt.


I'm asking for an enable_trace=True which would be like echo=True 
except *in addition* would do the equivalent[1] of logging with 
exc_info=true.


cheers,

Chris

[1] I'm not sure you could actually use exc_info=True, since no 
exception will have been raised at this point, you just need a repr of 
the current strack trace.


--
Simplistix - Content Management, Batch Processing  Python Consulting
- http://www.simplistix.co.uk
-- 
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] Hi,I found a codec bug,and I write a patch.

2010-01-13 Thread Gaetan de Menten
On Wed, Jan 13, 2010 at 10:16, 诚子 zhicheng1...@gmail.com wrote:
 detial please see

 http://my.unix-center.net/~WeiZhicheng/2010/01/13/sqlalchemy-0-6-unicodedecodeerror-bug-patch/

Well, if I understand correctly, you would like a workaround for a bad
truncate algorithm in mysql (which apparently truncates in the middle
of a multi-byte character). Are you sure it's not a configuration
problem of mysql? I would be amazed if they don't provide a solution
to this problem (though that wouldn't be the first time I'm amazed at
mysql... sigh...).

Now, as to solve your problem (assuming it's not solvable on mysql's
side), I think passing another UnicodeDecodingError behavior for the
decoder would be a better generic solution. This is not yet possible
with the generic types on current SQLAlchemy.

See http://www.sqlalchemy.org/trac/ticket/1257

In the meantime, you are free to use a custom type, or type decorator
to provide whatever custom decoding mechanism you want. We can't
accept your patch though, as it is too specific to your case and would
provide a very unexpected behavior in some cases (what if the decoding
error is not at the end of the string?).

Hope it helps,
-- 
Gaëtan de Menten
http://openhex.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.




Re: [sqlalchemy] Hi,I found a codec bug,and I write a patch.

2010-01-13 Thread 诚子
yeah,maybe you are right,
but I'm not professional in mysql
I just use the SQLAlchemy default
create the table and field
the field is utf8_general_ci

my problem like that

 import sqlalchemy
 sqlalchemy.__version__
'0.6beta1'
 from sqlalchemy import create_engine
 engine = create_engine('mysql://root:@localhost/dev')
 from sqlalchemy import Table, Column, Integer, String, Unicode,
MetaData, ForeignKey
 metadata = MetaData()
 users_table = Table('users', metadata,
... Column('id', Integer, primary_key=True),
... Column('name', Unicode(24)),
... )
 metadata.create_all(engine)
 from sqlalchemy.orm import sessionmaker
 Session = sessionmaker(bind=engine)
 session = Session()
 class User(object):
... def __init__(self, name):
... self.name = name
...
 from sqlalchemy.orm import mapper
 mapper(User, users_table)
Mapper at 0x101b81510; User
 name1 = '中文中文'
 name1
'\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87'
 len(name1)
12
 ed_user = User(name1.decode('utf8'))
 session.add(ed_user)
 session.commit()
 session.query(User).get(1).name
u'\u4e2d\u6587\u4e2d\u6587'
 name2 = '中文中文中文中文'
 name2
'\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87'
 ed_user = User(name2.decode('utf8'))
 session.add(ed_user)
 session.commit()
 session.query(User).get(2).name
'\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87'
 name3 = '中文中a文中文中文'
 name3
'\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87'
 ed_user = User(name3.decode('utf8'))
 session.add(ed_user)
 session.commit()
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/SQLAlchemy-0.6beta1dev-py2.6.egg/sqlalchemy/engine/default.py:207:
Warning: Data truncated for column 'name' at row 1
  cursor.execute(statement, parameters)
 session.query(User).get(3).name
Traceback (most recent call last):
...
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 22-23:
unexpected end of data


I know the last Chinese char is not complete
the end byte \x87 is not save to database.
the \xe6\x96\x87 is a complete Chinese char
and now is \xe6\x96 so when use the sqlalchemy
query It's can't decode by utf8. my solution
will ignore the end char \x96 and \xe6
the decode is work,and haven't Garbled.
if you don't care this you can ignore this issue
because it's not will appear in normal.

btw,in my phpMyAdmin the field is garbled,
but i use the django model hasn't this issue



2010/1/13 Gaetan de Menten gdemen...@gmail.com

 On Wed, Jan 13, 2010 at 10:16, 诚子 zhicheng1...@gmail.com wrote:
  detial please see
 
 
 http://my.unix-center.net/~WeiZhicheng/2010/01/13/sqlalchemy-0-6-unicodedecodeerror-bug-patch/http://my.unix-center.net/%7EWeiZhicheng/2010/01/13/sqlalchemy-0-6-unicodedecodeerror-bug-patch/

 Well, if I understand correctly, you would like a workaround for a bad
 truncate algorithm in mysql (which apparently truncates in the middle
 of a multi-byte character). Are you sure it's not a configuration
 problem of mysql? I would be amazed if they don't provide a solution
 to this problem (though that wouldn't be the first time I'm amazed at
 mysql... sigh...).

 Now, as to solve your problem (assuming it's not solvable on mysql's
 side), I think passing another UnicodeDecodingError behavior for the
 decoder would be a better generic solution. This is not yet possible
 with the generic types on current SQLAlchemy.

 See http://www.sqlalchemy.org/trac/ticket/1257

 In the meantime, you are free to use a custom type, or type decorator
 to provide whatever custom decoding mechanism you want. We can't
 accept your patch though, as it is too specific to your case and would
 provide a very unexpected behavior in some cases (what if the decoding
 error is not at the end of the string?).

 Hope it helps,
 --
 Gaëtan de Menten
 http://openhex.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.comsqlalchemy%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.






-- 
my.unix-center.net/~WeiZhicheng
-- 

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] array column as primary key

2010-01-13 Thread Michael Bayer
avdd wrote:
 I want to map a table with a postgresql array as a primary key.
 PostgreSQL supports it, and everything works until the session wants
 to use the list returned from the query as an instance key.   How can
 I intercept the row returned to wrap it in a tuple?  I can't figure
 out translate_row!

id skip translate_row and instead use TypeDecorator around PGArray.  
you'd override process_result_value to return tuple(value).




 
 from sqlalchemy.orm import sessionmaker
 from sqlalchemy import Column, Integer, create_engine
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.dialects.postgresql.base import ARRAY

 engine = create_engine('postgresql:///avdd')
 DB = sessionmaker(bind=engine)

 class A(declarative_base()):
 __tablename__ = 'a'
 ids = Column(ARRAY(Integer()), primary_key=True)

 A.__table__.delete(bind=engine).execute()

 a = A()
 a.ids = (1,2,3)

 db = DB()
 db.add(a)
 db.commit()

 del a, db

 db = DB()
 print db.query(A).all()
 

 Traceback (most recent call last):
   File testarraypk.py, line 25, in module
 print db.query(A).all()
   File lib/sqlalchemy/orm/query.py, line 1217, in all
 return list(self)
   File lib/sqlalchemy/orm/query.py, line 1376, in instances
 rows = [process[0](row, None) for row in fetch]
   File lib/sqlalchemy/orm/mapper.py, line 1681, in _instance
 instance = session_identity_map.get(identitykey)
   File lib/sqlalchemy/orm/identity.py, line 145, in get
 state = dict.get(self, key, default)
 TypeError: unhashable type: 'list'
 --
 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] tracking back from logged sql back to the python that caused it

2010-01-13 Thread Michael Bayer
Chris Withers wrote:
 Michael Bayer wrote:
 Chris Withers wrote:
 Easiest way to trap it is to monkeypatch Session.rollback(), or
 whatever
 rollback you think you might be calling, with a def that calls
 pdb.set_trace() or dumps a stack trace using inspect().
 Any chance of SA growing the ability to do the latter itself?

 Again, I really don't know what you mean here, unless you're asking for
 a
 enable_pdb=True keyword on rollback() or something, which I doubt.

 I'm asking for an enable_trace=True which would be like echo=True
 except *in addition* would do the equivalent[1] of logging with
 exc_info=true.

 cheers,

 Chris

 [1] I'm not sure you could actually use exc_info=True, since no
 exception will have been raised at this point, you just need a repr of
 the current strack trace.

when is a stack trace emitted ?

seems very much like something an external library should be doing.





 --
 Simplistix - Content Management, Batch Processing  Python Consulting
  - http://www.simplistix.co.uk
 --
 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] Case statement troubles

2010-01-13 Thread aoeuhtns
Hello,

I am relatively new to SQLAlchemy, and I am only really interested in
the SQL Expression Language for now.  I am having trouble creating
case statements.

SQLAlchemy version: 0.5.7
Python version: 2.6.2

Here is the error I am getting:

 type(mco)
class 'sqlalchemy.schema.Table'
 type(mco.c.abbreviation)
class 'sqlalchemy.schema.Column'
 x = case([(mco.c.abbreviation != None, mco.c.abbreviation)])
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: 'Table' object is not callable

The actual statement I want to create is more complicated, but this is
a simplified version.  I've tried a few random ideas, but I keep
getting this message.

Thanks
-- 
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: Case statement troubles

2010-01-13 Thread aoeuhtns
Oops!

 type(case)
class 'sqlalchemy.schema.Table'

I was looking at the case statement parameters, and ignoring what
Python was *really* complaining about!  I gave the case table a
different name and now I am back on track.  By the way, it's been a
real pleasure learning SQLAlchemy given the excellent documentation.

On Jan 13, 10:46 am, aoeuhtns psalvat...@gmail.com wrote:
 Hello,

 I am relatively new to SQLAlchemy, and I am only really interested in
 the SQL Expression Language for now.  I am having trouble creating
 case statements.

 SQLAlchemy version: 0.5.7
 Python version: 2.6.2

 Here is the error I am getting:

  type(mco)

 class 'sqlalchemy.schema.Table' type(mco.c.abbreviation)

 class 'sqlalchemy.schema.Column' x = case([(mco.c.abbreviation != None, 
 mco.c.abbreviation)])

 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: 'Table' object is not callable

 The actual statement I want to create is more complicated, but this is
 a simplified version.  I've tried a few random ideas, but I keep
 getting this message.

 Thanks
-- 
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] Hi,I found a codec bug,and I write a patch.

2010-01-13 Thread Ilia Kharin
If you use utf8 encoding in database you must set your character set in
connection url e.g.:
 engine = create_engine('mysql://root:@localhost/dev?charset=utf8')

...

 name1 = '中文中文'
 name1
'\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87'
 print name1
中文中文
 ed_user = User(name1)
 session.add(ed_user)
 session.commit()
-- 

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] Hi,I found a codec bug,and I write a patch.

2010-01-13 Thread 诚子
thanks, You are right!

2010/1/14 Ilia Kharin aksc...@gmail.com

 If you use utf8 encoding in database you must set your character set in
 connection url e.g.:
  engine = create_engine('mysql://root:@localhost/dev?charset=utf8')

 ...

  name1 = '中文中文'
  name1
 '\xe4\xb8\xad\xe6\x96\x87\xe4\xb8\xad\xe6\x96\x87'
  print name1
 中文中文
  ed_user = User(name1)
   session.add(ed_user)
  session.commit()

 --
 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.comsqlalchemy%2bunsubscr...@googlegroups.com
 .
 For more options, visit this group at
 http://groups.google.com/group/sqlalchemy?hl=en.




-- 
my.unix-center.net/~WeiZhicheng
-- 

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: array column as primary key

2010-01-13 Thread avdd
Thanks!  Works great.

On Jan 14, 2:27 am, Michael Bayer mike...@zzzcomputing.com wrote:
 avdd wrote:
  I want to map a table with a postgresql array as a primary key.
  PostgreSQL supports it, and everything works until the session wants
  to use the list returned from the query as an instance key.   How can
  I intercept the row returned to wrap it in a tuple?  I can't figure
  out translate_row!

 id skip translate_row and instead use TypeDecorator around PGArray.  
 you'd override process_result_value to return tuple(value).



  
  from sqlalchemy.orm import sessionmaker
  from sqlalchemy import Column, Integer, create_engine
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.dialects.postgresql.base import ARRAY

  engine = create_engine('postgresql:///avdd')
  DB = sessionmaker(bind=engine)

  class A(declarative_base()):
      __tablename__ = 'a'
      ids = Column(ARRAY(Integer()), primary_key=True)

  A.__table__.delete(bind=engine).execute()

  a = A()
  a.ids = (1,2,3)

  db = DB()
  db.add(a)
  db.commit()

  del a, db

  db = DB()
  print db.query(A).all()
  

  Traceback (most recent call last):
    File testarraypk.py, line 25, in module
      print db.query(A).all()
    File lib/sqlalchemy/orm/query.py, line 1217, in all
      return list(self)
    File lib/sqlalchemy/orm/query.py, line 1376, in instances
      rows = [process[0](row, None) for row in fetch]
    File lib/sqlalchemy/orm/mapper.py, line 1681, in _instance
      instance = session_identity_map.get(identitykey)
    File lib/sqlalchemy/orm/identity.py, line 145, in get
      state = dict.get(self, key, default)
  TypeError: unhashable type: 'list'
  --
  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] Problem installing SQLAlchemy as a dependency??

2010-01-13 Thread Lynton Grice
Hi there,

I am trying to package a small Python lib I have created that has an
install dependency on SQLAlchemy.

I have created my setup.py file with the install_requires

install_requires=[
'SQLAlchemy',
  ],

But whenever I try install my Python lib on a machine without
SQLAlchemy I get the following error:

Searching for SQLAlchemy
Reading http://pypi.python.org/simple/SQLAlchemy/
No local packages or download links found for SQLAlchemy
error: Could not find suitable distribution for Requirement.parse
('SQLAlchemy')

I have also tried all the SQLAlchemy=0.4.3 etc stuff and still no
luck.

I then decided to try install another Python lib - Elixir - and I get
the same issue

Processing dependencies for Elixir==0.8.0dev-r0
Searching for SQLAlchemy=0.5.0
Reading http://pypi.python.org/simple/SQLAlchemy/
No local packages or download links found for SQLAlchemy=0.5.0
error: Could not find suitable distribution for Requirement.parse
('SQLAlchemy=0.5.0')


Does anyone know how I can get this SQLAlchemy dependency to work on
the install?

Any help or advice will be greatly appreciated ;-)

Thanks

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