[sqlalchemy] pyodbc + FreeTDS segfault?? on linux

2011-04-06 Thread Randy Syring
I am running Ubuntu 10.04, python 2.6.5, SA 0.6.6, latest pyodbc
release.  I have tried FreeTDS that ships with the distro (0.82) as
well as current CVS.  I can make a connection and issue a basic SQL
statement.  However, when I try to run my unit tests, I get the
following error:

*** glibc detected *** /path/to/venv/bin/python: free(): invalid next
size (fast): 0x02527bf0 ***

The SQL issued just before that error is:

2011-04-07 00:26:39,257 INFO sqlalchemy.engine.base.Engine.0x...0350
INSERT INTO corporate_sessions (createdts, updatedts, id, hits,
relatedip, user_id) VALUES (?, ?, ?, ?, ?, ?)
2011-04-07 00:26:39,257 INFO sqlalchemy.engine.base.Engine.0x...0350
(datetime.datetime(2011, 4, 7, 0, 26, 39, 257073), None,
u'e1457896AdZgRjqg8CD8', 0, '10.66.3.230', 1)

and then the process just hangs and I have to kill the process.

My unit tests run successfully against sqlite, postgresql, and MSSQL
on Windows.  I have successfully ran the following test script on with
the same libraries and virtualenv:

import datetime
import sqlalchemy as sa
eng = sa.create_engine("mssql://user:pass@server/temp?
Port=1435&TDS_Version=8.0",echo=True)
res = eng.execute('select 1+1 as foo')
for row in res:
  print 'answer=',row['foo']

#eng.execute('DROP TABLE satest')
#eng.execute('CREATE TABLE satest(id INT, name VARCHAR(300), ts
datetime)')

res = eng.execute('INSERT INTO satest(id, name, ts) VALUES (?, ?, ?)',
  (10, u'foobar', datetime.datetime(2011, 4, 7, 0, 26,
39, 257073)))

One last thing, when I first tested this, I got an error related to
using an ORM instance when it had already been deleted (or something
like that, I can't remember exactly).  But I haven't seen that error
for a long time and don't remember doing anything in particular to
change it.

-- 
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] how to delete record (special case)

2011-04-06 Thread Michael Bayer

On Apr 6, 2011, at 5:43 AM, farcat wrote:

> Hello,
> 
> I am experimenting with a pattern where records hold the table name
> and record id of the next record in any other table, chaining records
> in different tables. This works, but I can't figure out how to clean
> op references to the next record in another table when I delete a
> record (the pattern does not use foreign keys in the normal sense).

.. in that it doesn't use foreign keys.Since you're working against the 
relational database's supported patterns, you'd need to roll the deletion of 
related rows yourself.The pattern is also called a "polymorphic 
association" and I blogged about it years ago here:  
http://techspot.zzzeek.org/2007/05/29/polymorphic-associations-with-sqlalchemy/ 
.




> The code is:
> 
> =
> 
> from sqlalchemy import *
> from sqlalchemy.orm.session import sessionmaker
> from sqlalchemy.ext.declarative import declarative_base,
> declared_attr, DeclarativeMeta
> #---
> Base = declarative_base()
> reg = dict()
> engine = create_engine('sqlite:///:memory:', echo=False)
> Session = sessionmaker(bind = engine)
> #---
> 
> class chainmeta(DeclarativeMeta):
> #---
>class Base(object):
>session = Session()
>@declared_attr
>def __tablename__(cls):
>return cls.__name__
> 
>id = Column(Integer, primary_key = True)
>next_table = Column(String(64))
>next_id = Column(Integer) #in table with name stored in
> next_table!
> 
>def __init__(self, data, next = None):
>self.data = data
>self.prev = None
>self.next = next
>self.session.add(self)
>self.session.flush()
> 
>def _getnext(self):
>if self.next_table and self.next_id:
> 
>return
> self.session.query(reg[self.next_table]).filter(self.next_id ==
> reg[self.next_table].id).one()
>else: return None
> 
>def _setnext(self, next):
>if next:
>if self.next:
>self.next.prev = None
>self.next_table = next.__tablename__
>self.next_id = next.id
>next.prev = self
>elif self.next:
>self.next.prev = None
>self.next_table = None
>self.next_id = None
> 
>def _delnext(self):
>self.next.prev = None
>self.next_table = None
>self.next_id = None
> 
>next = property(_getnext, _setnext, _delnext)
> 
>def __repr__(self):
>out = "type: " + type(self).__name__ + "["
>for name in self.__dict__:
>out += name + ", "
>out += "]"
>return out
> #---
>def __new__(mcls, name, coltype):
>return DeclarativeMeta.__new__(mcls, name, (chainmeta.Base,
> Base),{"data": Column(coltype, nullable = False)})
>def __init__(cls, name, coltype):
>reg[name] = cls
>return DeclarativeMeta.__init__(cls, name, (chainmeta.Base,
> Base),{})
> #---
> if __name__ == '__main__':
>Base.metadata.drop_all(engine)
>session = chainmeta.Base.session = Session()
> 
>Ni = chainmeta("Ni", Integer)
>Nb = chainmeta("Nb", Boolean)
>Nt = chainmeta("Nt", String(200))
>Base.metadata.create_all(engine)
> 
>ni1 = Ni(5)
>ni2 = Ni(12)
>nb1 = Nb(True)
>nb2 = Nb(False)
>nt1 = Nt("text in nt1")
>nt2 = Nt("text in nt2")
>ni1.next = ni2
>ni2.next = nb1
>nb1.next = nb2
>nb2.next = nt1
>nt1.next = nt2
>nt2.next = ni1 #circular
>print "OBJECTS"
>n = ni1
>count = 0
>print "nexts: ."
>while n and count < 10:
>print n.data
>count += 1
>n = n.next
>n = ni1
>count = 0
>print "prevs: ."
>while n and count < 10:
>print n.data
>count += 1
>n = n.prev
>print
> "---"
>nts = session.query(Nt).all()
>print "QUERIES"
>for nt in nts:
>print nt.data
>print "+"
>print session.query(Ni).filter(Ni.next_id ==
> nb1.id).first().data
> 
> 
> =
> 
> This might seem to have no reasonable us case, but it is something I
> want to use in a more complicated pattern later on. Basically the
> question is, how can I remove a record and have no next or prev
> pointing to it in other objects or records (without adding some sort
> of external controller)?
> 
> Chee

Re: [sqlalchemy] when using sqlachemy autoload feature with oracle table column name coming in lower case even if in DB it is in upper case

2011-04-06 Thread Michael Bayer

On Apr 6, 2011, at 3:54 PM, Anoop wrote:

> Hi All
> 
>   I am using Sqlalchemy 0.6.6 + cx_oracle5.0.3+python 2.6.4 and
> oracle 10g from linux.
> 
>  when I tried the sqlalchemy autoload feature
> 
>  tbl=Table('autoload_test', meta,
> autoload=True,autoload_with=engine, schema=None)
> 
>  Here I am getting tbl.columns.keys() are all lower case even if
> my column names are in upper case in DB . I checked the issue and
> found the reason
> 
>  in get_columns method (sqlalchemy/dialects/oracle/base.py)
> after fetching the column names in the loop before assigning the
> column name to colname variable normalize_name(sqlalchemy/dialects/
> oracle/base.py) method is calling and finally the colname varaiable
> will set as a value of name key and finally the dict will append to a
> list in the loop.

SQLAlchemy considers all lower case names to indicate a "case insensitive" 
name, whereas Oracle has the opposite behavior - it represents case insensitive 
names as all uppercase.SQLAlchemy's behavior in this regard is described at 
http://www.sqlalchemy.org/docs/dialects/oracle.html#identifier-casing .   The 
"normalize_name" method you've found is part of the implementation of this 
approach.

> 
> Here the problem is lc_value!=value checking ,suppose my column name
> is 'FIRST_NAME'

If your column is named "FIRST_NAME" in the Oracle database, and was named 
without quotes, the identifier is case insensitive.   Any casing will match it. 
  If you've named it "FIRST_NAME" and you did actually use quotes in the CREATE 
TABLE statement, Oracle still considers that name to be equivalent to 
"first_name", as long as quoting isn't used - because all uppercase is case 
insensitive in Oracle.  See note #8 at 
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements008.htm
  (I also tried here to confirm).


>   I am getting table column names in upper case (ie how they are  in
> DB, here I am not using any quoted column names') . . Now SQLServer
> +SqlAlchemy +autoload give upper case column names in upper case but

because SQL Server uses lowercase names to indicate "case insensitive".  Oracle 
does the opposite.

> when connecting with oracle upper case column names will be converted
> to lower case.Anybody have an idea why requires_quotes method is
> called like this?

Because SQLAlchemy is normalizing the differences in casing conventions to work 
across all databases with no changes in the declared Python metadata needed.  


-- 
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: when is object.id initialized

2011-04-06 Thread Michael Bayer

On Apr 6, 2011, at 6:38 AM, farcat wrote:

> Thank you,
> 
> I now experiment with putting  session.add and session.flush in
> object.__init__ ..
> 
> Are there any general disadvantages of that approach?

Using add() inside of __init__ is somewhat common.   Using flush() has the 
significant downside that flushes occur too often which is wasteful and 
performs poorly for larger scale operations (like bulk loads and such).   The 
ORM is designed such that primary key identifiers are not needed to be 
explicitly accessed outside of a flush except for query situations that wish to 
avoid the usage of relationships.   When you build your application to be 
strongly dependent on primary key identifiers being available within otherwise 
fully pending object graphs, you begin to work against the usage goals of the 
ORM.


> 
> 
> 
> On Apr 3, 7:44 pm, Michael Bayer  wrote:
>> Integer primary key identifiers are generated by the database itself using a 
>> variety of techniques which are all database-dependent.  This process occurs 
>> when the session flushes.
>> 
>> If you read the object relational tutorial starting 
>> athttp://www.sqlalchemy.org/docs/orm/tutorial.html#setting-up-the-mappingand 
>> working down through the end 
>> ofhttp://www.sqlalchemy.org/docs/orm/tutorial.html#adding-new-objectsyou 
>> will see that this interaction is described.
>> 
>> You can of course set .id to any value you'd like and that will be the value 
>> used when the flush happens.
>> 
>> On Apr 3, 2011, at 1:09 PM, farcat wrote:
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>>> Hi all,
>> 
>>> I use a kind of dynamic reference from parent_table to other tables.
>>> For that parent_table uses columns table_name and a record_id. This
>>> makes it possible to have a reference from parent_table to any record
>>> in any table in the database. However, say that i want to reference a
>>> record of table_name, i need the record.id to initialize
>>> parent_table.record_id. However, when i create a record and
>>> session.add it to the database, record.id == None.
>> 
>>> I was wondering when and how record.id is initialized and how it can
>>> be forced.
>> 
>>> Cheers, Lars
>> 
>>> --
>>> 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 
>>> 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 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] when using sqlachemy autoload feature with oracle table column name coming in lower case even if in DB it is in upper case

2011-04-06 Thread Anoop
Hi All

   I am using Sqlalchemy 0.6.6 + cx_oracle5.0.3+python 2.6.4 and
oracle 10g from linux.

  when I tried the sqlalchemy autoload feature

  tbl=Table('autoload_test', meta,
autoload=True,autoload_with=engine, schema=None)

  Here I am getting tbl.columns.keys() are all lower case even if
my column names are in upper case in DB . I checked the issue and
found the reason

  in get_columns method (sqlalchemy/dialects/oracle/base.py)
after fetching the column names in the loop before assigning the
column name to colname variable normalize_name(sqlalchemy/dialects/
oracle/base.py) method is calling and finally the colname varaiable
will set as a value of name key and finally the dict will append to a
list in the loop.

 cdict = {

'name': colname,

'type': coltype,

'nullable': nullable,

'default': default,

}

columns.append(cdict)

 Here In normalize_name method

   the code is
+
if name.upper() == name and \

   not
self.identifier_preparer._requires_quotes(name.lower()):

  return name.lower()

else:

   return name
++


the  _requires_quotes(sqlalchemy/sql/compiler.py) method will return a
Boolean value
++
def _requires_quotes(self, value):

"""Return True if the given identifier requires quoting."""

lc_value = value.lower()

return (lc_value in self.reserved_words

or value[0] in self.illegal_initial_characters

or not self.legal_characters.match(unicode(value))

or (lc_value != value))
++

 Here the problem is lc_value!=value checking ,suppose my column name
is 'FIRST_NAME' in normalize_name method we called this method
"self.identifier_preparer._requires_quotes(name.lower())" ,so value
='first_name'
and lc_value = value.lower() => 'fist_name'

* In this case the last checking 'first_name'!='first_name' will
always fail and all four condition in my case is false now so
_requires_quotes will return False
e
*  so now in normalize_name methodreturn name.lower() will invoke
and i will get column name in upper case.
t
when I modified the code in normalize_name method like below

if name.upper() == name and \

   not self.identifier_preparer._requires_quotes(name):

  return name.lower()

else:

   return name


   I am getting table column names in upper case (ie how they are  in
DB, here I am not using any quoted column names') . . Now SQLServer
+SqlAlchemy +autoload give upper case column names in upper case but
when connecting with oracle upper case column names will be converted
to lower case.Anybody have an idea why requires_quotes method is
called like this?

  thanks:
  Anoop

-- 
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: trouble with metaclass

2011-04-06 Thread lars van gemerden
On Mon, Apr 4, 2011 at 7:08 PM, Chris Withers wrote:

> On 23/03/2011 09:14, lars van gemerden wrote:
>
>>- I want to generate classes/tables based on metadata in another
>>table in the database, filled by "designers" not programmers.
>>
>
> Then you should be creating Tables and Mappers, not attempting to use
> Declarative.


I've had a closer look, can you explain what I can do with Tables en
Mappers, that I cannot with Declarative or otherwise what the advantage can
be?

>
>
> - Multiple inheritance (from the designer perspective) provides
>>flexibility in contracts ~ data attributes an object is sure to
>> provide.
>>
>
> I don't know what this means, please explain.


OK, bit vague, so say we have classes A, B and C(A,B) multiple inheriting
from A and B. Say also that A has members a1 and a2 (A adheres to a contract
saying it can provide a1 and a2) and B has b1 and b2. We can then give an
object of type C to methods built to handle objects of type A  (expecting a1
and a2) *and *to methods built to handle objects of type B  (expecting b1
and b2) . E.g. a "print" method might expect formatting attributes, while a
"save" method might expect a file type attribute.

Forget about the designer perspective, it even confuses me now ... :-)

>
>
> - The diamond problem would complicate matters too much for
>>
>
> I don't know what "the diamond problem" this, please explain.
>
> http://en.wikipedia.org/wiki/Diamond_problem: not really a problem, but
added complexity that most likely will go past the sweet spot between
simplicity and power for my use case.

>
> - SQLalchemy does not provide multiple inheritance (just single) out
>>of the box, as far as i have been able to figure out.
>>
>
> Did you look at Table Inheritance?
> I think for your needs that Declarative is a confusing distraction.
> I'd avoid it...


I did, it seems to have pretty good support for single inheritance, but not
for multiple inheritance. I haven't seen a way to adapt it (and Michael
Bayer didn't seem to either without a lot of complexity ...  :-( )

>
>
>  There are more requirements, like in the end having (persistent) objects
>> that still work like python classes for basic programming.
>>
>
> Yep, sounds like tables, model classes and mappers to bridge the two to
> me...
>
>
>  Not all problems can be solved with a simplistix approach ;-).
>>
>
> The Simplistix approach has always been to find the simplest solution that
> solves all the requirements. You still appear to be going in for an
> extremely complex solution to a problem you don't fully understand...


I definitively want too avoid any unnecessary complexity (i go through my
code for clean-up cycles with increasing understanding of the subject at
hand). Coming more from the OO side then the DB side, declarative just felt
more intuitive to start with. It also seems to mesh better with metaclasses
and since I need to generate tables/classes that are unknown before run time


Any way, thanks for the feed back. There are some Mapper configurations that
interest me,  but I don't quite get them yet.

Cheers, Lars



>
> cheers,
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>   - http://www.simplistix.co.uk
>



-- 

Lars van Gemerden
l...@rational-it.com
+31 6 26 88 55 39


-- 
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: when is object.id initialized

2011-04-06 Thread farcat
Thank you,

I now experiment with putting  session.add and session.flush in
object.__init__ ..

Are there any general disadvantages of that approach?



On Apr 3, 7:44 pm, Michael Bayer  wrote:
> Integer primary key identifiers are generated by the database itself using a 
> variety of techniques which are all database-dependent.  This process occurs 
> when the session flushes.
>
> If you read the object relational tutorial starting 
> athttp://www.sqlalchemy.org/docs/orm/tutorial.html#setting-up-the-mappingand 
> working down through the end 
> ofhttp://www.sqlalchemy.org/docs/orm/tutorial.html#adding-new-objectsyou will 
> see that this interaction is described.
>
> You can of course set .id to any value you'd like and that will be the value 
> used when the flush happens.
>
> On Apr 3, 2011, at 1:09 PM, farcat wrote:
>
>
>
>
>
>
>
> > Hi all,
>
> > I use a kind of dynamic reference from parent_table to other tables.
> > For that parent_table uses columns table_name and a record_id. This
> > makes it possible to have a reference from parent_table to any record
> > in any table in the database. However, say that i want to reference a
> > record of table_name, i need the record.id to initialize
> > parent_table.record_id. However, when i create a record and
> > session.add it to the database, record.id == None.
>
> > I was wondering when and how record.id is initialized and how it can
> > be forced.
>
> > Cheers, Lars
>
> > --
> > 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 
> > 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 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] how to delete record (special case)

2011-04-06 Thread farcat
Hello,

I am experimenting with a pattern where records hold the table name
and record id of the next record in any other table, chaining records
in different tables. This works, but I can't figure out how to clean
op references to the next record in another table when I delete a
record (the pattern does not use foreign keys in the normal sense).
The code is:

=

from sqlalchemy import *
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.ext.declarative import declarative_base,
declared_attr, DeclarativeMeta
#---
Base = declarative_base()
reg = dict()
engine = create_engine('sqlite:///:memory:', echo=False)
Session = sessionmaker(bind = engine)
#---

class chainmeta(DeclarativeMeta):
#---
class Base(object):
session = Session()
@declared_attr
def __tablename__(cls):
return cls.__name__

id = Column(Integer, primary_key = True)
next_table = Column(String(64))
next_id = Column(Integer) #in table with name stored in
next_table!

def __init__(self, data, next = None):
self.data = data
self.prev = None
self.next = next
self.session.add(self)
self.session.flush()

def _getnext(self):
if self.next_table and self.next_id:

return
self.session.query(reg[self.next_table]).filter(self.next_id ==
reg[self.next_table].id).one()
else: return None

def _setnext(self, next):
if next:
if self.next:
self.next.prev = None
self.next_table = next.__tablename__
self.next_id = next.id
next.prev = self
elif self.next:
self.next.prev = None
self.next_table = None
self.next_id = None

def _delnext(self):
self.next.prev = None
self.next_table = None
self.next_id = None

next = property(_getnext, _setnext, _delnext)

def __repr__(self):
out = "type: " + type(self).__name__ + "["
for name in self.__dict__:
out += name + ", "
out += "]"
return out
#---
def __new__(mcls, name, coltype):
return DeclarativeMeta.__new__(mcls, name, (chainmeta.Base,
Base),{"data": Column(coltype, nullable = False)})
def __init__(cls, name, coltype):
reg[name] = cls
return DeclarativeMeta.__init__(cls, name, (chainmeta.Base,
Base),{})
#---
if __name__ == '__main__':
Base.metadata.drop_all(engine)
session = chainmeta.Base.session = Session()

Ni = chainmeta("Ni", Integer)
Nb = chainmeta("Nb", Boolean)
Nt = chainmeta("Nt", String(200))
Base.metadata.create_all(engine)

ni1 = Ni(5)
ni2 = Ni(12)
nb1 = Nb(True)
nb2 = Nb(False)
nt1 = Nt("text in nt1")
nt2 = Nt("text in nt2")
ni1.next = ni2
ni2.next = nb1
nb1.next = nb2
nb2.next = nt1
nt1.next = nt2
nt2.next = ni1 #circular
print "OBJECTS"
n = ni1
count = 0
print "nexts: ."
while n and count < 10:
print n.data
count += 1
n = n.next
n = ni1
count = 0
print "prevs: ."
while n and count < 10:
print n.data
count += 1
n = n.prev
print
"---"
nts = session.query(Nt).all()
print "QUERIES"
for nt in nts:
print nt.data
print "+"
print session.query(Ni).filter(Ni.next_id ==
nb1.id).first().data


=

This might seem to have no reasonable us case, but it is something I
want to use in a more complicated pattern later on. Basically the
question is, how can I remove a record and have no next or prev
pointing to it in other objects or records (without adding some sort
of external controller)?

Cheers, Lars


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