[sqlalchemy] Re: Operational Error raised by except_

2011-02-22 Thread neurino
I guess since, I learn it now, EXCEPT is not supported by MySQL...

I guess I'll have to change my query at all...

On Feb 22, 12:57 pm, neurino neur...@gmail.com wrote:
 I have now problems with except_ in MySQL: the code that worked flawlessly
 in sqlite now causes an error, seems right after EXCEPT in query:

 ProgrammingError: (ProgrammingError) (1064, You have an error in your SQL
 syntax; check the manual that corresponds to your MySQL server version for
 the right syntax to use near 'SELECT sensors.id_cu AS sensors_id_cu,
 sensors.id_meas AS sensors_id_meas, senso' at line 3) 'SELECT
 anon_1.sensors_id_cu AS anon_1_sensors_id_cu, anon_1.sensors_id_meas AS
 anon_1_sensors_id_meas, anon_1.sensors_id_elab AS anon_1_sensors_id_elab,
 anon_1.sensors_name AS anon_1_sensors_name, anon_1.sensors_desc AS
 anon_1_sensors_desc \nFROM (SELECT sensors.id_cu AS sensors_id_cu,
 sensors.id_meas AS sensors_id_meas, sensors.id_elab AS sensors_id_elab,
 sensors.name AS sensors_name, sensors.`desc` AS sensors_desc \nFROM sensors
 EXCEPT SELECT sensors.id_cu AS sensors_id_cu, sensors.id_meas AS
 sensors_id_meas, sensors.id_elab AS sensors_id_elab, sensors.name AS
 sensors_name, sensors.`desc` AS sensors_desc \nFROM sensors, view_opts
 \nWHERE sensors.id_cu = view_opts.id_cu AND sensors.id_meas =
 view_opts.id_meas AND view_opts.id_view = %s AND view_opts.id IS NOT NULL
 ORDER BY sensors.id_cu, sensors.id_meas) AS anon_1 ORDER BY
 anon_1.sensors_id_cu, anon_1.sensors_id_meas' (1L,)

 I'm using:
  * sqlalchemy 0.6.6
  * MySQLdb 1.2.3
  * MySQL Ver 14.14 Distrib 5.1.41,

 Thanks for your support

 2011/1/13 neurino neur...@gmail.com







  Thanks Michael,

  just for following readers I precise the ORDER BY clause causing the
  OperationalError is the one coming *before* the EXCEPT so I had to
  add .order_by(None) to the first query, now it looks like:

  Session.query(model.Sensor) \
     .order_by(None) \
      .except_(
         Session.query(model.Sensor) \
         .filter(model.Sensor.id_cu==model.ViewOpt.id_cu) \
         .filter(model.Sensor.id_meas==model.ViewOpt.id_meas) \
         .filter(model.ViewOpt.id_view==1)
         )

  and works perfectly, thanks again!

  Cheers
  neurino

  On Jan 12, 5:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
   On Jan 12, 2011, at 11:20 AM, neurino wrote:

Well as I wrote ordering involves everything, also forms creation with
formalchemy (make a select where all sensors are ordered that way etc)
anyway I understand your point of view.

quickest is a where sensor id not in (query), as a simple WHERE
  clause

Problem comes when Sensor primary key is composite (id_cu +
id_meas)...

The good 'ol python comes in handy anyway:

all = Session.query(model.Sensor).all()
selected = Session.query(model.Sensor).filter(
... model.Sensor.id_cu==model.ViewOpt.id_cu).filter(
... model.Sensor.id_meas==model.ViewOpt.id_meas).filter(
... model.ViewOpt.id_view==1).all()
diff = [sens for sens in all if sens not in selected]
len(all), len(selected), len(diff)
(154, 6, 148)

We're talking of working on max total 200/300 sensors.

The OR way did not filter anything (maybe I made somwthing wrong).

   Oh you know what, I completely forgot the best solution.  It *is*
  documented on query.order_by() though which is an argument forchecking!
     pass None to query.order_by().  That disables all order_by's for that
  query.  So go back to your except_() and use except_(q.order_by(None)).

Greetings

On Jan 12, 4:04 pm, Michael Bayer mike...@zzzcomputing.com wrote:
On Jan 12, 2011, at 8:46 AM, neurino wrote:

I need always the same order_by in all app and it could be subject of
modification and / or integration in the near future so which better
place than mapper to define it once instead of any time I do a query?

It sounds like the ordering here is for the purposes of view logic so
  I'd have view logic that is factored down to receive Query objects that
  return Sensor rows, the view logic then applies the .order_by() to the
  Query.   I.e. in a web app I use a Paginator object of some kind that does
  this, given a Query.   This is probably a reason I don't like order_by to
  be within mapper(), it doesn't define persistence, rather a view.

Anyway do you think there are alternate paths to get `all sensors but
already choosen` which are order_by compatible?

quickest is a where sensor id not in (query), as a simple WHERE
  clause, or use OR, query sensor where sensor.cu != cu OR sensor.meas !=
  meas OR sensor.view  != view.   Except is not as widely used and I think
  its not even supported by all backends, even though it is a nice logical set
  operator, its got annoying quirks like this one.

Thanks for your support

On Jan 12, 2:38 pm, Michael Bayer mike...@zzzcomputing.com wrote:
On Jan 12, 2011, at 7:28 AM, neurino wrote:

I have this model:

``I organize 

[sqlalchemy] Unable to select on Table subclass

2011-02-22 Thread bool
I wrote a simple subclass of Table


class MyTable(Table):

def __init__(self, name, metadata, *args, **kwargs):


super(MyTable, self).__init__(name, metadata, *args, **kwargs)


def select(self, whereclause=None):
return super(BitemporalTable, self).select(self.c.z  1)
==

select(t) where t is an object of MyTable is giving this error. What
is the solution...

Traceback (most recent call last):
  File t.py, line 16, in module
select(t)
  File /prod/tools/infra/SQLAlchemy-0.5.5-py2.5.egg/sqlalchemy/sql/
expression.py, line 246, in select
s = Select(columns, whereclause=whereclause, from_obj=from_obj,
**kwargs)
  File /prod/tools/infra/SQLAlchemy-0.5.5-py2.5.egg/sqlalchemy/sql/
expression.py, line 3239, in __init__
[_literal_as_column(c) for c in columns]
TypeError: 'MyTable' object is not iterable

-- 
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: Unable to select on Table subclass

2011-02-22 Thread bool
  def select(self, whereclause=None):
return super(MyTable, self).select(self.c.z  1)


You can ignore this method. This error comes with out this method
also.

-- 
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] SqlAlchemy+cx_oracle decimal point problem with stddev

2011-02-22 Thread Massi
Hi everyone, in my script (python 2.6, Oracle10g, cx_oracle 5.0.4,
sqlalchemy 0.6.5) I'm running the following simple query on one of my
tables:

table = Table(my_data_table, metadata, autoload=True)
col = getattr(table.c, my_integer_col)
res = select(func.stdev(col)).execute().fetchone()

where my_integer_col obviously contains only int values.
I get the following error:
...
  File C:\Python26\lib\site-packages\sqlalchemy\engine\base.py, line
2445, in _fetchone_impl
return self.cursor.fetchone()
  File C:\Python26\lib\site-packages\sqlalchemy\dialects\oracle
\cx_oracle.py, line 496, in maybe_decimal
return int(value)
ValueError: invalid literal for int() with base 10:
'18,89258326656747167219869520430353668307'

I think this is related with the usage of the comma decimal point in
the results, but the error is raised by the cx_oracle module so I
cannot handle it. is this a bug or am I missing something?
Thanks in advance!

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



Re: [sqlalchemy] Unable to select on Table subclass

2011-02-22 Thread Michael Bayer
Table is not meant for subclassing, so you would need to read the source code 
to understand these issues.   The issue below cannot be reproduced:

from sqlalchemy import *

class MyTable(Table):

def __init__(self, name, metadata, *args, **kwargs):
super(MyTable, self).__init__(name, metadata, *args, **kwargs)


def bar(self):
 return hi

m = MetaData()
mt = MyTable('foo', m, Column('id', Integer))

print mt.bar()


0.7 output:
hi

0.6 output:
hi

0.5 output:
hi



On Feb 22, 2011, at 8:30 AM, bool wrote:

 I wrote a simple subclass of Table
 
 
 class MyTable(Table):
 
def __init__(self, name, metadata, *args, **kwargs):


super(MyTable, self).__init__(name, metadata, *args, **kwargs)
 
 
def select(self, whereclause=None):
return super(BitemporalTable, self).select(self.c.z  1)
 ==
 
 select(t) where t is an object of MyTable is giving this error. What
 is the solution...
 
 Traceback (most recent call last):
  File t.py, line 16, in module
select(t)
  File /prod/tools/infra/SQLAlchemy-0.5.5-py2.5.egg/sqlalchemy/sql/
 expression.py, line 246, in select
s = Select(columns, whereclause=whereclause, from_obj=from_obj,
 **kwargs)
  File /prod/tools/infra/SQLAlchemy-0.5.5-py2.5.egg/sqlalchemy/sql/
 expression.py, line 3239, in __init__
[_literal_as_column(c) for c in columns]
 TypeError: 'MyTable' object is not iterable
 
 -- 
 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.
 

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



Re: [sqlalchemy] SqlAlchemy+cx_oracle decimal point problem with stddev

2011-02-22 Thread Michael Bayer

Regarding the comma, set NLS_LANG to a locale that uses a decimal point, or use 
0.6.6, this behavior is described in the third paragraph at 
http://www.sqlalchemy.org/docs/dialects/oracle.html#precision-numerics .





On Feb 22, 2011, at 8:54 AM, Massi wrote:

 Hi everyone, in my script (python 2.6, Oracle10g, cx_oracle 5.0.4,
 sqlalchemy 0.6.5) I'm running the following simple query on one of my
 tables:
 
 table = Table(my_data_table, metadata, autoload=True)
 col = getattr(table.c, my_integer_col)
 res = select(func.stdev(col)).execute().fetchone()
 
 where my_integer_col obviously contains only int values.
 I get the following error:
 ...
  File C:\Python26\lib\site-packages\sqlalchemy\engine\base.py, line
 2445, in _fetchone_impl
return self.cursor.fetchone()
  File C:\Python26\lib\site-packages\sqlalchemy\dialects\oracle
 \cx_oracle.py, line 496, in maybe_decimal
return int(value)
 ValueError: invalid literal for int() with base 10:
 '18,89258326656747167219869520430353668307'
 
 I think this is related with the usage of the comma decimal point in
 the results, but the error is raised by the cx_oracle module so I
 cannot handle it. is this a bug or am I missing something?
 Thanks in advance!
 
 -- 
 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.
 

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



Re: [sqlalchemy] Unable to select on Table subclass

2011-02-22 Thread Michael Bayer

On Feb 22, 2011, at 8:30 AM, bool wrote:

 
 select(t) where t is an object of MyTable is giving this error. What
 is the solution...

also, I recommend upgrading to a modern release of SQLAlchemy.  The above usage 
produces this error:

sqlalchemy.exc.ArgumentError: columns argument to select() must be a Python 
list or other iterable

that is, select([t]).   See 
http://www.sqlalchemy.org/docs/core/tutorial.html#selecting .

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



Re: [sqlalchemy] Re: Operational Error raised by except_

2011-02-22 Thread neurino
Something like this:

stmt = Session.query(model.ViewOpt.id_cu, model.ViewOpt.id_meas) \
.filter(model.ViewOpt.id_view==1).subquery()

query = Session.query(model.Sensor) \
.outerjoin((stmt,
and_(model.Sensor.id_cu==stmt.c.id_cu,
 model.Sensor.id_meas==stmt.c.id_meas))) \
.filter(and_(stmt.c.id_cu==None, stmt.c.id_meas==None))

Cheers


2011/2/22 neurino neur...@gmail.com

 I guess since, I learn it now, EXCEPT is not supported by MySQL...

 I guess I'll have to change my query at all...

 On Feb 22, 12:57 pm, neurino neur...@gmail.com wrote:
  I have now problems with except_ in MySQL: the code that worked
 flawlessly
  in sqlite now causes an error, seems right after EXCEPT in query:
 
  ProgrammingError: (ProgrammingError) (1064, You have an error in your
 SQL
  syntax; check the manual that corresponds to your MySQL server version
 for
  the right syntax to use near 'SELECT sensors.id_cu AS sensors_id_cu,
  sensors.id_meas AS sensors_id_meas, senso' at line 3) 'SELECT
  anon_1.sensors_id_cu AS anon_1_sensors_id_cu, anon_1.sensors_id_meas AS
  anon_1_sensors_id_meas, anon_1.sensors_id_elab AS anon_1_sensors_id_elab,
  anon_1.sensors_name AS anon_1_sensors_name, anon_1.sensors_desc AS
  anon_1_sensors_desc \nFROM (SELECT sensors.id_cu AS sensors_id_cu,
  sensors.id_meas AS sensors_id_meas, sensors.id_elab AS sensors_id_elab,
  sensors.name AS sensors_name, sensors.`desc` AS sensors_desc \nFROM
 sensors
  EXCEPT SELECT sensors.id_cu AS sensors_id_cu, sensors.id_meas AS
  sensors_id_meas, sensors.id_elab AS sensors_id_elab, sensors.name AS
  sensors_name, sensors.`desc` AS sensors_desc \nFROM sensors, view_opts
  \nWHERE sensors.id_cu = view_opts.id_cu AND sensors.id_meas =
  view_opts.id_meas AND view_opts.id_view = %s AND view_opts.id IS NOT
 NULL
  ORDER BY sensors.id_cu, sensors.id_meas) AS anon_1 ORDER BY
  anon_1.sensors_id_cu, anon_1.sensors_id_meas' (1L,)
 
  I'm using:
   * sqlalchemy 0.6.6
   * MySQLdb 1.2.3
   * MySQL Ver 14.14 Distrib 5.1.41,
 
  Thanks for your support
 
  2011/1/13 neurino neur...@gmail.com
 
 
 
 
 
 
 
   Thanks Michael,
 
   just for following readers I precise the ORDER BY clause causing the
   OperationalError is the one coming *before* the EXCEPT so I had to
   add .order_by(None) to the first query, now it looks like:
 
   Session.query(model.Sensor) \
  .order_by(None) \
   .except_(
  Session.query(model.Sensor) \
  .filter(model.Sensor.id_cu==model.ViewOpt.id_cu) \
  .filter(model.Sensor.id_meas==model.ViewOpt.id_meas) \
  .filter(model.ViewOpt.id_view==1)
  )
 
   and works perfectly, thanks again!
 
   Cheers
   neurino
 
   On Jan 12, 5:28 pm, Michael Bayer mike...@zzzcomputing.com wrote:
On Jan 12, 2011, at 11:20 AM, neurino wrote:
 
 Well as I wrote ordering involves everything, also forms creation
 with
 formalchemy (make a select where all sensors are ordered that way
 etc)
 anyway I understand your point of view.
 
 quickest is a where sensor id not in (query), as a simple WHERE
   clause
 
 Problem comes when Sensor primary key is composite (id_cu +
 id_meas)...
 
 The good 'ol python comes in handy anyway:
 
 all = Session.query(model.Sensor).all()
 selected = Session.query(model.Sensor).filter(
 ... model.Sensor.id_cu==model.ViewOpt.id_cu).filter(
 ... model.Sensor.id_meas==model.ViewOpt.id_meas).filter(
 ... model.ViewOpt.id_view==1).all()
 diff = [sens for sens in all if sens not in selected]
 len(all), len(selected), len(diff)
 (154, 6, 148)
 
 We're talking of working on max total 200/300 sensors.
 
 The OR way did not filter anything (maybe I made somwthing wrong).
 
Oh you know what, I completely forgot the best solution.  It *is*
   documented on query.order_by() though which is an argument
 forchecking!
  pass None to query.order_by().  That disables all order_by's for
 that
   query.  So go back to your except_() and use except_(q.order_by(None)).
 
 Greetings
 
 On Jan 12, 4:04 pm, Michael Bayer mike...@zzzcomputing.com
 wrote:
 On Jan 12, 2011, at 8:46 AM, neurino wrote:
 
 I need always the same order_by in all app and it could be
 subject of
 modification and / or integration in the near future so which
 better
 place than mapper to define it once instead of any time I do a
 query?
 
 It sounds like the ordering here is for the purposes of view logic
 so
   I'd have view logic that is factored down to receive Query objects that
   return Sensor rows, the view logic then applies the .order_by() to the
   Query.   I.e. in a web app I use a Paginator object of some kind that
 does
   this, given a Query.   This is probably a reason I don't like
 order_by to
   be within mapper(), it doesn't define persistence, rather a view.
 
 Anyway do you think there are alternate paths to get `all sensors
 but
 already choosen` which are 

[sqlalchemy] Create Database trought sqlalchemy 0.6.6 !

2011-02-22 Thread Toninho Nunes
Hi,

I would like to know how to create database with sqlalchemy using the
PostGresql driver, are there a sample or example?

sqlalchemy just only works with database postgresql previous created.

see my code:

import sqlalchemy
from sqlalchemy import create_engine, Table, MetaData, Integer,
String, ForeignKey, Column, LargeBinary

db_engine = create_engine('postgresql+psycopg2://
postgres:magi1850@localhost/newscom')

metadata = MetaData()

metadata.create_all(db_engine)

Error message:

sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database
newscom2 does not exist
 None None

Any ideas ?

Toninho Nunes

-- 
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: adjacency via table

2011-02-22 Thread farcat
thought i'd post the code I came up with for reference:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.associationproxy import association_proxy

Base = declarative_base()

def _create_inheritance(supertype, subtype):
return Inheritance(supertype, subtype)

inheritance_table = Table('inheritance', Base.metadata,
Column('sub_name', String(50), ForeignKey('types.name'),
primary_key=True),
Column('super_name', String(50), ForeignKey('types.name'),
primary_key=True))

class Type(Base):
__tablename__ = 'types'
name = Column(String(50), primary_key=True)
abstract = Column(Boolean)
top = Column(Boolean)
subtypes = relationship('Type',
secondary=inheritance_table,
primaryjoin=inheritance_table.c.super_name==name,
secondaryjoin=inheritance_table.c.sub_name==name,
backref='supertypes')
def hasSuper(self):
return self.supertypes.length  0
def hasSub(self):
return self.subtypes.length  0
def isAnySubOf(self, tp):
#to check for cyclic inheritance
if self == tp:
return True
for typ in self.supertypes:
if typ.isAnySubOf(tp):
return True
return False
def isAnySuperOf(self, tp):
return tp.isAnySubOf(self)
def addSub(self, tp):
#some types cannot have supertypes:
if not tp.top:
#to check for cyclic inheritance:
if not self.isAnySubOf(tp):
self.subtypes.append(tp)
else: raise Exception(cyclic inheritance)
else: raise Exception(str(tp) +  cannot have supertype)
def addSuper(self, tp):
tp.addSub(self)
def __init__(self, name, top = False, abstract = False):
self.name = name
self.top = top
self.abstract = abstract
def __repr__(self):
return Type(%r) % (self.name)

engine = create_engine('sqlite:///:memory:', echo=False)
Base.metadata.create_all(engine)


if __name__ == __main__:
Session = sessionmaker(bind=engine)
session = Session()
try:
c1 = Type(A)
c2 = Type(B)
c3 = Type(C)
c1.addSub(c2)
c3.addSuper(c1)
c3.addSuper(c2)
c3.addSub(c1) #= would be cyclic
print c1.subs
for c in c1.subtypes:
print   + str(c)
print c1.supers
for c in c1.supertypes:
print   +str(c)
print c2.subs
for c in c2.subtypes:
print   +  str(c)
print c2.supers
for c in c2.supertypes:
print   + str(c)
print c3.subs
for c in c3.subtypes:
print   + str(c)
print c3.supers
for c in c3.supertypes:
print   + str(c)
except Exception, e:
   print error:  + str(e)



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



Re: [sqlalchemy] self-referential relationship w/ declarative style

2011-02-22 Thread Michael Bayer

On Feb 20, 2011, at 10:12 PM, Ryan wrote:

 I'm attempting a self-referential mapping on a Client object that includes 
 these two columns:
 
 id = Column(Integer, primary_key=True)
 inviter_id = Column(Integer, ForeignKey('users.id'), nullable=True)
 
 Started here with no luck:
 
 inviter = relationship('Client', primaryjoin='Client.id == 
 Client.inviter_id', uselist=False)
 
 Then read about self-referential mapping in the docs and tried with no luck:
 
 inviter = relationship('Client', remote_side='Client.id', uselist=False)
 
 And this with an error:
 
 relationship('Client', remote_side=[Client.id], uselist=False)
 
 Would be a great help to see how this is done in a declarative style. Thanks! 


the last example in the section 
http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships
 illustrates a declarative self-referential relationship.  Note that the id 
Column object can be referenced directly when you're inside the class 
declaration itself.


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

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



Re: [sqlalchemy] Is it possible to have single transaction across several http calls?

2011-02-22 Thread Michael Bayer

Holding open a transaction across several web requests is in general a bad 
idea.  HTTP is stateless - the requests could be spaced hours apart or not at 
all, leaving the transaction hanging open permanently.   While the transaction 
is open, locks may be held, preventing concurrent activities upon the affected 
rows from proceeding.   

That said, one simple way to do this would be to stow away the 
transaction-holding object, assuming its a Session(), in some kind of storage 
(most likely a dictionary) where it is accessed by each successive request.   
The full series of requests would need to take place in a single process.

A more robust method, if you're on Postgresql or MySQL, would be to defer the 
commit of several individual transactions using two phase xids.   This would 
enable a collection of per-connection transactions to participate in a larger 
two phase operation.   Each request emits the PREPARE command with a distinct 
xid, storing the transaction's state on disk.   Then COMMIT PREPARED is emitted 
for each.  Docs on this for Postgresql are at 
http://www.postgresql.org/docs/current/static/sql-prepare-transaction.html .   
The SQLAlchemy Connection object can provide a conversation like this as 
follows:

from sqlalchemy import *

e = create_engine('postgresql://scott:tiger@localhost/test', echo=True)
e.execute(drop table if exists foo)
e.execute(create table foo(id integer))

c = e.connect()

t1 = c.begin_twophase()
c.execute(insert into foo (id) values (1))
t1.prepare()
c.close()

c = e.connect()
t2 = c.begin_twophase()
c.execute(insert into foo (id) values (2))
t2.prepare()
c.close()

c = e.connect()
t3 = c.begin_twophase()
c.execute(insert into foo (id) values (3))
t3.prepare()
c.close()


c = e.connect()
for t in (t1, t2, t3):
c.commit_prepared(t.xid, recover=True)

print e.execute(select * from foo).fetchall()


If using that with a Session, you'd need to embed the Session in the 
transaction using a scheme similar to the one described at 
http://www.sqlalchemy.org/docs/orm/session.html#joining-a-session-into-an-external-transaction
 (just not within the context of a unit test). 





On Feb 21, 2011, at 12:35 AM, Andrey Gladilin wrote:

 I have a big RESTfull API in my python web application using Werkzeug
 and SQLAlchemy. Could you advise some way to use a single transaction
 across several http calls?
 
 Thanks!
 
 -- 
 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.
 

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



Re: [sqlalchemy] commit() okay on every web request ?

2011-02-22 Thread Michael Bayer
calling commit() on every request is fine, as long as you aren't creating 
unwanted 'dirty' state inadvertently.Watching your SQL logs could help you 
to determine if things are happening that are undesirable.

On Feb 21, 2011, at 3:38 AM, Romy wrote:

 Switched to autocommit=False, and calling commit(), followed by
 remove() on every end-of-request, regardless of whether there's data
 to commit or not.
 
 Am I adding any unnecessary overhead ? And if so, how should I be
 checking for dirtyness prior to committing ?
 
 R
 
 -- 
 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.
 

-- 
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] Small docs problem

2011-02-22 Thread Christoph Zwerschke
Just noticed that the 0.6.6 docs show a name parameter of subquery(),
but it does not yet seem to be available in 0.6.6. There should be a
note that it can only be used in 0.6.7 or 0.7.

-- Christoph

-- 
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: Create Database trought sqlalchemy 0.6.6 !

2011-02-22 Thread Eric Ongerth
sqlalchemy allows you to issue any literal sql statements as text:

http://www.sqlalchemy.org/docs/core/tutorial.html#using-text


On Feb 22, 7:38 am, Toninho Nunes toninhonu...@gmail.com wrote:
 Hi,

 I would like to know how to create database with sqlalchemy using the
 PostGresql driver, are there a sample or example?

 sqlalchemy just only works with database postgresql previous created.

 see my code:

 import sqlalchemy
 from sqlalchemy import create_engine, Table, MetaData, Integer,
 String, ForeignKey, Column, LargeBinary

 db_engine = create_engine('postgresql+psycopg2://
 postgres:magi1850@localhost/newscom')

 metadata = MetaData()

 metadata.create_all(db_engine)

 Error message:

 sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database
 newscom2 does not exist
  None None

 Any ideas ?

 Toninho Nunes

-- 
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: Create Database trought sqlalchemy 0.6.6 !

2011-02-22 Thread Eric Ongerth
Even with that autocommit transaction isolation level, you probably
need to commit the create database before you try to add tables to
it.

On Feb 22, 1:45 pm, Toninho Nunes toninhonu...@gmail.com wrote:
 Hi see my source code below

 import sqlalchemy
 import psycopg2
 from sqlalchemy import create_engine, Table, MetaData, Integer,
 String, ForeignKey, Column, LargeBinary
 from sqlalchemy.sql import text

 db_engine = create_engine('postgresql+psycopg2://
 postgres:password@localhost/newscom3', echo=True)
 db_engine.raw_connection().set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
 db_engine.text(CREATE DATABASE newscom3 WITH OWNER = postgres
 ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'pt_BR.utf8'
 LC_CTYPE = 'pt_BR.utf8' CONNECTION LIMIT = -1;).execute()

 metadata = MetaData()
 metadata.create_all(db_engine)

 I run the source code, but I receive the following error.

 Traceback (most recent call last):
   File newscomddl.py, line 18, in module

 db_engine.raw_connection().set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/engine/base.py, line 1874, in raw_connection
     return self.pool.unique_connection()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 142, in unique_connection
     return _ConnectionFairy(self).checkout()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 369, in __init__
     rec = self._connection_record = pool.get()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 213, in get
     return self.do_get()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 732, in do_get
     con = self.create_connection()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 147, in create_connection
     return _ConnectionRecord(self)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 253, in __init__
     self.connection = self.__connect()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 319, in __connect
     connection = self.__pool._creator()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/engine/strategies.py, line 82, in connect
     return dialect.connect(*cargs, **cparams)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/engine/default.py, line 249, in connect
     return self.dbapi.connect(*cargs, **cparams)
 sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database
 newscom3 does not exist
  None None

 I don't get know where are wrong, could you help me?

 Thanks,

 Toninho Nunes

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



Re: [sqlalchemy] PostgreSQL BIT type not properly supported by dialect

2011-02-22 Thread Michael Bayer
this has been fixed in 0.7, however it is completely a mystery how the change 
got in there, and it doesn't have test coverage. I'm thinking perhaps 
someone on IRC handed me a patch or something and I forgot it was there since 
it was committed along with something not really related:

http://www.sqlalchemy.org/trac/changeset/7154#file5

so that's your patch, if you want to provide tests we can also backport to 0.6. 
 The actual change is a two liner.




On Feb 22, 2011, at 8:54 PM, Rami Chowdhury wrote:

 Hi,
 
 We're using PostgreSQL's BIT type to store bit-strings 
 (http://www.postgresql.org/docs/current/static/datatype-bit.html), and (at 
 least as of SQLA 0.6.6) the PostgreSQL dialect's BIT type doesn't seem to 
 support those kinds of fields -- it just issues BIT when creating the 
 tables, and we get 1-bit fields! At the moment (for table creation at least) 
 we're getting around this using the following:
 
 
 class PG_BIT(sqlalchemy.dialects.postgresql.BIT):
 
 
 def __init__(self, length=None, *args, **kw):
 
 
 # Takes one positional arg, the bit length. Can be omitted
 
 
 self._bit_length = length
 
 
 
 
 @sqlalchemy.ext.compiler.compiles(PG_BIT)
 
 
 def compile_PG_BIT(element, compiler, **kw):
 
 
 if element._bit_length:
 
 
 return BIT(%d) % element._bit_length
 
 
 else:
 
 
 return compiler.visit_BIT(element, **kw)
 
 
 and using PG_BIT everywhere we'd want to use BIT. Obviously, though, it'd be 
 great if SQLAlchemy supported this better. I'm very happy to try and work up 
 a patch myself, but I'd appreciate advice on :
- what I should try to patch (postgresql.BIT and 
 postgresql.base.PGTypeCompiler are my first guesses)
- what kind of coding / testing practices I should observe
- should I try and write a patch against hg tip? against the 0.6.6 release?
- is there anything I'm missing?
 
 Thanks!
 Rami
 
 -- 
 Rami Chowdhury
 Never assume malice when stupidity will suffice. -- Hanlon's Razor
 +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544
 
 -- 
 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.

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



Re: [sqlalchemy] self-referential relationship w/ declarative style

2011-02-22 Thread Michael Bayer

On Feb 22, 2011, at 9:03 PM, Ryan McKillen wrote:

 Mike, thanks a lot. Big help. I'm almost there.
 
 This seems to do the trick:
 
 usersid = Column(Integer, primary_key=True, key='id')
 inviter_id = Column(Integer, ForeignKey('users.id'))
 
 inviter = relationship('User',
 uselist = False,
 backref = backref('invitee', remote_side=usersid, uselist=True),
 )
 
 When there are two users, one being the inviter (parent) and the other being 
 the invitee (child), it works like a charm:
 
 self.assertEqual(invitee1.inviter.id, inviter.id)
 self.assertEqual(inviter.invitee[0].id, invitee1.id)
 
 But add a third user, one being the inviter and two being the invitees, 
 invitee1.inviter is None.

probably because of that uselist=False, which makes it into a one-to-one. 
Adjacency list is a standard single foreign key relationship - one-to-many on 
one side, many-to-one on the other.

There's an illustration of exactly how the data resides in the table:

http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships



 
 Any ideas for me?
 
 
 On Tue, Feb 22, 2011 at 7:54 AM, Michael Bayer mike...@zzzcomputing.com 
 wrote:
 
 On Feb 20, 2011, at 10:12 PM, Ryan wrote:
 
 I'm attempting a self-referential mapping on a Client object that includes 
 these two columns:
 
 id = Column(Integer, primary_key=True)
 inviter_id = Column(Integer, ForeignKey('users.id'), nullable=True)
 
 Started here with no luck:
 
 inviter = relationship('Client', primaryjoin='Client.id == 
 Client.inviter_id', uselist=False)
 
 Then read about self-referential mapping in the docs and tried with no luck:
 
 inviter = relationship('Client', remote_side='Client.id', uselist=False)
 
 And this with an error:
 
 relationship('Client', remote_side=[Client.id], uselist=False)
 
 Would be a great help to see how this is done in a declarative style. 
 Thanks! 
 
 
 the last example in the section 
 http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships
  illustrates a declarative self-referential relationship.  Note that the id 
 Column object can be referenced directly when you're inside the class 
 declaration itself.
 
 
 
 
 -- 
 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.
 
 
 -- 
 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.
 
 
 -- 
 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.

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



Re: [sqlalchemy] Re: Create Database trought sqlalchemy 0.6.6 !

2011-02-22 Thread Michael Bayer
Setting an autocommit setting on a single raw_connection() won't work also 
because that's just one connection out of several in the pool.   The operation 
should be performed on a Connection:

c = engine.connect()
c.detach() # so it is never returned to the pool, since we're changing settings
c.connection.set_isolation_level(...)
c.execute(statement)


Will add this in 0.7 to psycopg2's allowed list of isolation modes:

c.execution_options(isolation_level='AUTOCOMMIT') - ticket #2072



On Feb 22, 2011, at 9:18 PM, Eric Ongerth wrote:

 Even with that autocommit transaction isolation level, you probably
 need to commit the create database before you try to add tables to
 it.
 
 On Feb 22, 1:45 pm, Toninho Nunes toninhonu...@gmail.com wrote:
 Hi see my source code below
 
 import sqlalchemy
 import psycopg2
 from sqlalchemy import create_engine, Table, MetaData, Integer,
 String, ForeignKey, Column, LargeBinary
 from sqlalchemy.sql import text
 
 db_engine = create_engine('postgresql+psycopg2://
 postgres:password@localhost/newscom3', echo=True)
 db_engine.raw_connection().set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
 db_engine.text(CREATE DATABASE newscom3 WITH OWNER = postgres
 ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'pt_BR.utf8'
 LC_CTYPE = 'pt_BR.utf8' CONNECTION LIMIT = -1;).execute()
 
 metadata = MetaData()
 metadata.create_all(db_engine)
 
 I run the source code, but I receive the following error.
 
 Traceback (most recent call last):
   File newscomddl.py, line 18, in module
 
 db_engine.raw_connection().set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/engine/base.py, line 1874, in raw_connection
 return self.pool.unique_connection()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 142, in unique_connection
 return _ConnectionFairy(self).checkout()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 369, in __init__
 rec = self._connection_record = pool.get()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 213, in get
 return self.do_get()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 732, in do_get
 con = self.create_connection()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 147, in create_connection
 return _ConnectionRecord(self)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 253, in __init__
 self.connection = self.__connect()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/pool.py, line 319, in __connect
 connection = self.__pool._creator()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/engine/strategies.py, line 82, in connect
 return dialect.connect(*cargs, **cparams)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
 py2.6.egg/sqlalchemy/engine/default.py, line 249, in connect
 return self.dbapi.connect(*cargs, **cparams)
 sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database
 newscom3 does not exist
  None None
 
 I don't get know where are wrong, could you help me?
 
 Thanks,
 
 Toninho Nunes
 
 -- 
 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.
 

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



Re: [sqlalchemy] self-referential relationship w/ declarative style

2011-02-22 Thread Ryan McKillen
I added that in because without it I get:
TypeError: Incompatible collection type: User is not list-like


On Tue, Feb 22, 2011 at 6:47 PM, Michael Bayer mike...@zzzcomputing.comwrote:


 On Feb 22, 2011, at 9:03 PM, Ryan McKillen wrote:

 Mike, thanks a lot. Big help. I'm almost there.

 This seems to do the trick:

 usersid = Column(Integer, primary_key=True, key='id')
 inviter_id = Column(Integer, ForeignKey('users.id'))

 inviter = relationship('User',
 uselist = False,
 backref = backref('invitee', remote_side=usersid, uselist=True),
 )

 When there are two users, one being the inviter (parent) and the other
 being the invitee (child), it works like a charm:

 self.assertEqual(invitee1.inviter.id, inviter.id)
 self.assertEqual(inviter.invitee[0].id, invitee1.id)

 But add a third user, one being the inviter and two being the invitees,
 invitee1.inviter is None.


 probably because of that uselist=False, which makes it into a one-to-one.
   Adjacency list is a standard single foreign key relationship - one-to-many
 on one side, many-to-one on the other.

 There's an illustration of exactly how the data resides in the table:


 http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships




 Any ideas for me?


 On Tue, Feb 22, 2011 at 7:54 AM, Michael Bayer 
 mike...@zzzcomputing.comwrote:


 On Feb 20, 2011, at 10:12 PM, Ryan wrote:

 I'm attempting a self-referential mapping on a Client object that includes
 these two columns:

 id = Column(Integer, primary_key=True)
 inviter_id = Column(Integer, ForeignKey('users.id'), nullable=True)


 Started here with no luck:

 inviter = relationship('Client', primaryjoin='Client.id ==
 Client.inviter_id', uselist=False)


 Then read about self-referential mapping in the docs and tried with no
 luck:

 inviter = relationship('Client', remote_side='Client.id', uselist=False)


 And this with an error:

 relationship('Client', remote_side=[Client.id], uselist=False)


 Would be a great help to see how this is done in a declarative style.
 Thanks!



 the last example in the section
 http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationshipsillustrates
  a declarative self-referential relationship.  Note that the id
 Column object can be referenced directly when you're inside the class
 declaration itself.




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



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



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


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


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



Re: [sqlalchemy] self-referential relationship w/ declarative style

2011-02-22 Thread Michael Bayer

one side scalar, one side collection.   the collection side you use .append().  
 You decide which end is the non-collection by setting remote_side, in your 
code below its invitee.


On Feb 22, 2011, at 9:59 PM, Ryan McKillen wrote:

 I added that in because without it I get:
 TypeError: Incompatible collection type: User is not list-like
 
 
 On Tue, Feb 22, 2011 at 6:47 PM, Michael Bayer mike...@zzzcomputing.com 
 wrote:
 
 On Feb 22, 2011, at 9:03 PM, Ryan McKillen wrote:
 
 Mike, thanks a lot. Big help. I'm almost there.
 
 This seems to do the trick:
 
 usersid = Column(Integer, primary_key=True, key='id')
 inviter_id = Column(Integer, ForeignKey('users.id'))
 
 inviter = relationship('User',
 uselist = False,
 backref = backref('invitee', remote_side=usersid, uselist=True),
 )
 
 When there are two users, one being the inviter (parent) and the other being 
 the invitee (child), it works like a charm:
 
 self.assertEqual(invitee1.inviter.id, inviter.id)
 self.assertEqual(inviter.invitee[0].id, invitee1.id)
 
 But add a third user, one being the inviter and two being the invitees, 
 invitee1.inviter is None.
 
 probably because of that uselist=False, which makes it into a one-to-one. 
 Adjacency list is a standard single foreign key relationship - one-to-many on 
 one side, many-to-one on the other.
 
 There's an illustration of exactly how the data resides in the table:
 
 http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships
 
 
 
 
 Any ideas for me?
 
 
 On Tue, Feb 22, 2011 at 7:54 AM, Michael Bayer mike...@zzzcomputing.com 
 wrote:
 
 On Feb 20, 2011, at 10:12 PM, Ryan wrote:
 
 I'm attempting a self-referential mapping on a Client object that includes 
 these two columns:
 
 id = Column(Integer, primary_key=True)
 inviter_id = Column(Integer, ForeignKey('users.id'), nullable=True)
 
 Started here with no luck:
 
 inviter = relationship('Client', primaryjoin='Client.id == 
 Client.inviter_id', uselist=False)
 
 Then read about self-referential mapping in the docs and tried with no luck:
 
 inviter = relationship('Client', remote_side='Client.id', uselist=False)
 
 And this with an error:
 
 relationship('Client', remote_side=[Client.id], uselist=False)
 
 Would be a great help to see how this is done in a declarative style. 
 Thanks! 
 
 
 the last example in the section 
 http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships
  illustrates a declarative self-referential relationship.  Note that the 
 id Column object can be referenced directly when you're inside the class 
 declaration itself.
 
 
 
 
 -- 
 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.
 
 
 -- 
 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.
 
 
 -- 
 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.
 
 
 -- 
 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.
 
 
 -- 
 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.

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



Re: [sqlalchemy] self-referential relationship w/ declarative style

2011-02-22 Thread Ryan McKillen
Got it. Many thanks!



On Tue, Feb 22, 2011 at 7:02 PM, Michael Bayer mike...@zzzcomputing.comwrote:


 one side scalar, one side collection.   the collection side you use
 .append().   You decide which end is the non-collection by setting
 remote_side, in your code below its invitee.


 On Feb 22, 2011, at 9:59 PM, Ryan McKillen wrote:

 I added that in because without it I get:
 TypeError: Incompatible collection type: User is not list-like


 On Tue, Feb 22, 2011 at 6:47 PM, Michael Bayer 
 mike...@zzzcomputing.comwrote:


 On Feb 22, 2011, at 9:03 PM, Ryan McKillen wrote:

 Mike, thanks a lot. Big help. I'm almost there.

 This seems to do the trick:

 usersid = Column(Integer, primary_key=True, key='id')
 inviter_id = Column(Integer, ForeignKey('users.id'))

 inviter = relationship('User',
 uselist = False,
 backref = backref('invitee', remote_side=usersid, uselist=True),
 )

 When there are two users, one being the inviter (parent) and the other
 being the invitee (child), it works like a charm:

 self.assertEqual(invitee1.inviter.id, inviter.id)
 self.assertEqual(inviter.invitee[0].id, invitee1.id)

 But add a third user, one being the inviter and two being the invitees,
 invitee1.inviter is None.


 probably because of that uselist=False, which makes it into a one-to-one.
 Adjacency list is a standard single foreign key relationship -
 one-to-many on one side, many-to-one on the other.

 There's an illustration of exactly how the data resides in the table:


 http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationships




 Any ideas for me?


 On Tue, Feb 22, 2011 at 7:54 AM, Michael Bayer 
 mike...@zzzcomputing.comwrote:


 On Feb 20, 2011, at 10:12 PM, Ryan wrote:

 I'm attempting a self-referential mapping on a Client object that
 includes these two columns:

 id = Column(Integer, primary_key=True)
 inviter_id = Column(Integer, ForeignKey('users.id'), nullable=True)


 Started here with no luck:

 inviter = relationship('Client', primaryjoin='Client.id ==
 Client.inviter_id', uselist=False)


 Then read about self-referential mapping in the docs and tried with no
 luck:

 inviter = relationship('Client', remote_side='Client.id', uselist=False)


 And this with an error:

 relationship('Client', remote_side=[Client.id], uselist=False)


 Would be a great help to see how this is done in a declarative style.
 Thanks!



 the last example in the section
 http://www.sqlalchemy.org/docs/orm/relationships.html#adjacency-list-relationshipsillustrates
  a declarative self-referential relationship.  Note that the id
 Column object can be referenced directly when you're inside the class
 declaration itself.




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



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



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



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



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


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


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

[sqlalchemy] Re: Create Database trought sqlalchemy 0.6.6 !

2011-02-22 Thread Toninho Nunes
You are right, but I would like to create the database without to
connect to
other database existent, I'm newbie learning python and sqlalchemy.
any example will be welcome.

Thanks a lot


On Feb 22, 6:06 pm, Warwick Prince warwi...@mushroomsys.com wrote:
 Hi Toninho

 Looks to me that the issue is that your original create_engine is connecting
 to the DB /newscom3, which you have not yet created. (i.e. you are just
 about to create it, but it does not exist YET).

 I'm not sure exactly (so forgive me if I'm wrong) but I would assume you
 would possibly need to connect to another DB and then create newcom3 and
 then create a new engine connected to the newly created DB..

 Worth a try anyway!  :-)

 Cheers
 Warwick

 On 23 February 2011 07:45, Toninho Nunes toninhonu...@gmail.com wrote:







  Hi see my source code below

  import sqlalchemy
  import psycopg2
  from sqlalchemy import create_engine, Table, MetaData, Integer,
  String, ForeignKey, Column, LargeBinary
  from sqlalchemy.sql import text

  db_engine = create_engine('postgresql+psycopg2://
  postgres:password@localhost/newscom3', echo=True)

  db_engine.raw_connection().set_isolation_level(psycopg2.extensions.ISOLATIO 
  N_LEVEL_AUTOCOMMIT)
  db_engine.text(CREATE DATABASE newscom3 WITH OWNER = postgres
  ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'pt_BR.utf8'
  LC_CTYPE = 'pt_BR.utf8' CONNECTION LIMIT = -1;).execute()

  metadata = MetaData()
  metadata.create_all(db_engine)

  I run the source code, but I receive the following error.

  Traceback (most recent call last):
   File newscomddl.py, line 18, in module

  db_engine.raw_connection().set_isolation_level(psycopg2.extensions.ISOLATIO 
  N_LEVEL_AUTOCOMMIT)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/engine/base.py, line 1874, in raw_connection
     return self.pool.unique_connection()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/pool.py, line 142, in unique_connection
     return _ConnectionFairy(self).checkout()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/pool.py, line 369, in __init__
     rec = self._connection_record = pool.get()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/pool.py, line 213, in get
     return self.do_get()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/pool.py, line 732, in do_get
     con = self.create_connection()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/pool.py, line 147, in create_connection
     return _ConnectionRecord(self)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/pool.py, line 253, in __init__
     self.connection = self.__connect()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/pool.py, line 319, in __connect
     connection = self.__pool._creator()
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/engine/strategies.py, line 82, in connect
     return dialect.connect(*cargs, **cparams)
   File /usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.6-
  py2.6.egg/sqlalchemy/engine/default.py, line 249, in connect
     return self.dbapi.connect(*cargs, **cparams)
  sqlalchemy.exc.OperationalError: (OperationalError) FATAL:  database
  newscom3 does not exist
   None None

  I don't get know where are wrong, could you help me?

  Thanks,

  Toninho Nunes

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

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