Re: [sqlalchemy] isolation_level and psycopg2?

2010-08-09 Thread Jon Nelson
On Mon, Aug 9, 2010 at 11:27 PM, Michael Bayer  wrote:
>
> On Aug 10, 2010, at 12:03 AM, Jon Nelson wrote:
>
>> I tried setting isolation_level to SERIALIZABLE in my create_engine
>> options, while using psycopg2.
>> However, an strace clearly shows that it is using READ COMMITTED.
>> Is setting the isolation_level not supported with psycopg2?
>
>
> Here's a test:
>
> eng = create_engine('postgresql://', isolation_level='SERIALIZABLE', 
> echo=True)
> print eng.execute('show transaction isolation level').scalar()
>
> for me it returns 'serializable'.

Aha, but in psycopg2, transactions ALWAYS get a new transaction level
(set on the psycopg2 connection object with set_isolation_level).  Add
to the above:


conn = eng.connect()
t = conn.begin()
print conn.execute('show transaction isolation level').scalar()

...

Mine shows:

read committed

And, of course, strace doesn't lie (much).

I have a patch! It's probably wrong, but it works (there should
probably be more checking on the actual *value* before we go about
using getattr, but here it is):


diff -r 753e46f6868c lib/sqlalchemy/dialects/postgresql/psycopg2.py
--- a/lib/sqlalchemy/dialects/postgresql/psycopg2.pyMon Aug 09
20:32:37 2010 -0400
+++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.pyMon Aug 09
23:37:10 2010 -0500
@@ -207,6 +207,12 @@
 psycopg = __import__('psycopg2')
 return psycopg

+def do_begin(self, conn):
+if self.isolation_level:
+extensions = __import__('psycopg2.extensions').extensions
+level = getattr(extensions, 'ISOLATION_LEVEL_' +
self.isolation_level.upper())
+conn.set_isolation_level(level)
+
 def on_connect(self):
 base_on_connect = super(PGDialect_psycopg2, self).on_connect()
 if self.dbapi and self.use_native_unicode:


-- 
Jon

-- 
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] isolation_level and psycopg2?

2010-08-09 Thread Michael Bayer

On Aug 10, 2010, at 12:03 AM, Jon Nelson wrote:

> I tried setting isolation_level to SERIALIZABLE in my create_engine
> options, while using psycopg2.
> However, an strace clearly shows that it is using READ COMMITTED.
> Is setting the isolation_level not supported with psycopg2?


Here's a test:

eng = create_engine('postgresql://', isolation_level='SERIALIZABLE', 
echo=True)
print eng.execute('show transaction isolation level').scalar()

for me it returns 'serializable'.  


-- 
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] isolation_level and psycopg2?

2010-08-09 Thread Jon Nelson
I tried setting isolation_level to SERIALIZABLE in my create_engine
options, while using psycopg2.
However, an strace clearly shows that it is using READ COMMITTED.
Is setting the isolation_level not supported with psycopg2?


-- 
See, when the GOVERNMENT spends money, it creates jobs; whereas when
the money is left in the hands of TAXPAYERS, God only knows what they
do with it. Bake it into pies, probably. Anything to avoid creating
jobs. - Dave Barry?

Jon

-- 
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] To select a few columns in some tables related

2010-08-09 Thread Michael Bayer

On Aug 9, 2010, at 6:26 PM, Alvaro Reinoso wrote:

> Hello,
> 
> I have these classes:
> 
>class Channel(rdb.Model):
>   rdb.metadata(metadata)
>   rdb.tablename("channels")
> 
>   id = Column("id", Integer, primary_key=True)
>   title = Column("title", String(100))
> 
> 
>   items = relationship("MediaItem", secondary=channel_items,
> order_by="MediaItem.titleView", backref="channels")
> 
>class MediaItem(rdb.Model):
>   rdb.metadata(metadata)
>   rdb.tablename("media_items")
> 
>   id = Column("id", Integer, primary_key=True)
>   title = Column("title", String(100))
> 
>class User(rdb.Model):
>   rdb.metadata(metadata)
>   rdb.tablename("users")
> 
>   id = Column("id", Integer, primary_key=True)
>   name = Column("name", String(50))
> 
>   channels = relationship("Channel", secondary=user_channels,
> order_by="Channel.titleView", backref="users")
> 
> MediaItem is related to Channel and Channel is related to User.
> 
> if I'd like to select some columns from items and channels, I'd do
> this:
> 
>session = Session()
>result =
> session.query(Channel).join(Channel.items).values(Channel.title,
> Item.title)
> 
> I get an instance of Channel class with its items.
> 
> My problem is I don't know how to select some columns from User,
> Channel and Item. How can I make a query where for example, I can
> select the User.name property and its channels with only Channel.title
> property and the items of those channels with only Item.title
> property?

the form illustrated in the second example of 
http://www.sqlalchemy.org/docs/ormtutorial.html#querying selects individual 
columns, as does the "values()" method you're already using (i.e. 
values(User.name, Channel.title, Item.title) ).   You'd also want to add an 
extra join for "Channel.users".

-- 
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] To select a few columns in some tables related

2010-08-09 Thread Alvaro Reinoso
Hello,

I have these classes:

class Channel(rdb.Model):
rdb.metadata(metadata)
rdb.tablename("channels")

id = Column("id", Integer, primary_key=True)
title = Column("title", String(100))


items = relationship("MediaItem", secondary=channel_items,
order_by="MediaItem.titleView", backref="channels")

class MediaItem(rdb.Model):
rdb.metadata(metadata)
rdb.tablename("media_items")

id = Column("id", Integer, primary_key=True)
title = Column("title", String(100))

class User(rdb.Model):
rdb.metadata(metadata)
rdb.tablename("users")

id = Column("id", Integer, primary_key=True)
name = Column("name", String(50))

channels = relationship("Channel", secondary=user_channels,
order_by="Channel.titleView", backref="users")

MediaItem is related to Channel and Channel is related to User.

if I'd like to select some columns from items and channels, I'd do
this:

session = Session()
result =
session.query(Channel).join(Channel.items).values(Channel.title,
Item.title)

I get an instance of Channel class with its items.

My problem is I don't know how to select some columns from User,
Channel and Item. How can I make a query where for example, I can
select the User.name property and its channels with only Channel.title
property and the items of those channels with only Item.title
property?

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 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] Automatically merge a specific model/object

2010-08-09 Thread Michael Bayer

On Aug 9, 2010, at 5:12 PM, flzz wrote:

> Greetings,  I have an object model which I'm persisting with
> SQLAlchemy (0.6.3).  I have a need for one of the objects in the model
> to always merge itself into the session when a new instance is added.
> For example, given
> 
>sub = Subscription(email=EmailAddress(value='f...@bar.com))
> 
> which will then be added to the model via a dynaimc_loader backed by
> an AppenderQuery
> 
>model.lists[0].subscriptions.append(sub)
> 
> Given the EmailAddress model enforces uniqueness of the value, I want
> the EmailAddress instance to merge itself into the session without me
> having to manually query for it.

If I'm understanding correctly, that's not as much about merging as it is for 
the "unique object" recipe, which is this:  
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject

which assumes the usage of a scoped_session.   Since you're looking for the 
callable EmailAddress to know about a session and you don't want to pass it in, 
there's not really any other option.



> 
> So far I've explored a SessionExtension and manually passing the
> session into the constructor using a trick I found while searching
> around on this group:
> 
>sub = Subscription(email=EmailAddress(session=session,
> value='f...@bar.com))



> 
> This does exactly what I want it to but is less than desirable from a
> usability perspective.  I've had no luck with a SessionExtension.  Any
> recommendations?
> 
> 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.
> 

-- 
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] Automatically merge a specific model/object

2010-08-09 Thread flzz
Greetings,  I have an object model which I'm persisting with
SQLAlchemy (0.6.3).  I have a need for one of the objects in the model
to always merge itself into the session when a new instance is added.
For example, given

sub = Subscription(email=EmailAddress(value='f...@bar.com))

which will then be added to the model via a dynaimc_loader backed by
an AppenderQuery

model.lists[0].subscriptions.append(sub)

Given the EmailAddress model enforces uniqueness of the value, I want
the EmailAddress instance to merge itself into the session without me
having to manually query for it.

So far I've explored a SessionExtension and manually passing the
session into the constructor using a trick I found while searching
around on this group:

sub = Subscription(email=EmailAddress(session=session,
value='f...@bar.com))

This does exactly what I want it to but is less than desirable from a
usability perspective.  I've had no luck with a SessionExtension.  Any
recommendations?

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] SQLAlchemy 0.6.4 Documentation: SQLite

2010-08-09 Thread Michael Bayer

On Aug 9, 2010, at 1:00 PM, BartonC wrote:

> There's a broken link on
> http://www.sqlalchemy.org/docs/reference/dialects/sqlite.htm
> to the old pysqlite project:
> Full documentation on pysqlite is available at:
> http://www.initd.org/pub/software/pysqlite/doc/usage-guide.html.
> 
> Can we get this update to link to:
> http://pysqlite.googlecode.com/svn/doc/sqlite3.html

current docs on pysqlite's own site point to:

http://docs.pysqlite.googlecode.com/hg/sqlite3.html

Would need to know where the official docs are.

If the link to pysqlite's docs are going to keep moving around (seeing that 
pysqlite doesn't have a domain name of its own), I might want to point to 
http://docs.python.org/library/sqlite3.html instead.


-- 
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] SQLAlchemy 0.6.4 Documentation: SQLite

2010-08-09 Thread BartonC
There's a broken link on
http://www.sqlalchemy.org/docs/reference/dialects/sqlite.htm
to the old pysqlite project:
Full documentation on pysqlite is available at:
http://www.initd.org/pub/software/pysqlite/doc/usage-guide.html.

Can we get this update to link to:
http://pysqlite.googlecode.com/svn/doc/sqlite3.html

Thank you.

-- 
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] Adding columns to SELECT clause without having to filter result entities

2010-08-09 Thread Michael Bayer

On Aug 9, 2010, at 10:56 AM, Nikolaj wrote:

> I have an association object pattern between Person and Item through
> PersonItem. Item is subclassed using single-table inheritance into
> FooItem and BarItem, with a column named 'foo' on FooItem (although
> it's obviously on the same shared table as BarItem).
> 
> I'd like to query PersonItem and joinedload(Person.item) with
> polymorphic columns. However, I end up resorting to the pattern on the
> last line below because I can't figure out how to add columns to the
> SELECT without having them appear in the result entities. Is there a
> way to accomplish this more easily?
> 
> results = Session.query(PersonItem) \
>.options(
>joinedload(PersonItem.person, innerjoin=True),
>joinedload(PersonItem.item, innerjoin=True)
>) \
>.add_columns(Item.__table__.c.foo) \
>.all()
> 
> if results: results = zip(*results)[0]

the proposed syntax for this is joinedload(PersonItem.item.of_type(FooItem, 
BarItem)), but that is not implemented at the moment.   The usual of_type() 
accessor only supports one subclass at the moment, and it is not implemented 
for loader options.



> 
> -- 
> 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] Adding columns to SELECT clause without having to filter result entities

2010-08-09 Thread Nikolaj
I have an association object pattern between Person and Item through
PersonItem. Item is subclassed using single-table inheritance into
FooItem and BarItem, with a column named 'foo' on FooItem (although
it's obviously on the same shared table as BarItem).

I'd like to query PersonItem and joinedload(Person.item) with
polymorphic columns. However, I end up resorting to the pattern on the
last line below because I can't figure out how to add columns to the
SELECT without having them appear in the result entities. Is there a
way to accomplish this more easily?

results = Session.query(PersonItem) \
.options(
joinedload(PersonItem.person, innerjoin=True),
joinedload(PersonItem.item, innerjoin=True)
) \
.add_columns(Item.__table__.c.foo) \
.all()

if results: results = zip(*results)[0]

-- 
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: Proper way to make a TypeDecorator around Enum?

2010-08-09 Thread Nikolaj
On Aug 9, 3:38 pm, Michael Bayer  wrote:

> its pulling off the "types.Enum" callable from self.__class__ (which actually 
> seems unnecessary here, self.impl should be fine), then calling that with the 
> given arguments.  So it should be setting up the native_enum=False part at 
> least.   The length attribute though would need to be set on impl.        
> There's no reason we can't make "length" an actual argument of Enum, though, 
> just to help with this kind of thing (I'm assuming that you're planning on 
> adding additional elements to the enum in the future and you don't want to 
> have to alter the column, correct ?).

Correct. About the self.length thing, I simply misread __getattr__ on
TypeDecorator as __setattr__. I've included my test below just for
completness' sake to show that everything works as expected.

from sqlalchemy import create_engine, Column, types
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base(bind=engine)
Session = scoped_session(sessionmaker(bind=engine))

class StringEnum(types.TypeDecorator):
impl = types.Enum

def __init__(self, *args, **kwargs):
super(StringEnum, self).__init__(*args, native_enum=False,
**kwargs)
self.impl.length = 255

class Item(Base):
__tablename__ = 'items'
name = Column(types.String, primary_key=True)
option = Column(StringEnum(('foo', 'bar')), nullable=True)

Base.metadata.create_all()

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



Re: [sqlalchemy] Re: Proper way to make a TypeDecorator around Enum?

2010-08-09 Thread Michael Bayer

On Aug 9, 2010, at 10:32 AM, Nikolaj wrote:

> It makes sense to subclass the type directly if I'm not messing with
> result or bind values. This works:
> 
> class StringEnum(types.Enum):
>def __init__(self, *args, **kwargs):
>super(StringEnum, self).__init__(*args, native_enum=False,
> **kwargs)
>self.length = 255
> 
> I'm still curious why the TypeDecorator I pasted previously doesn't
> work though. The superconstructor does the self.impl =
> self.__class__.impl(...) anyway so I figured this was the better
> approach than calling self.impl = Enum(...).

its pulling off the "types.Enum" callable from self.__class__ (which actually 
seems unnecessary here, self.impl should be fine), then calling that with the 
given arguments.  So it should be setting up the native_enum=False part at 
least.   The length attribute though would need to be set on impl.
There's no reason we can't make "length" an actual argument of Enum, though, 
just to help with this kind of thing (I'm assuming that you're planning on 
adding additional elements to the enum in the future and you don't want to have 
to alter the column, correct ?).


> 
> On Aug 9, 2:56 pm, Michael Bayer  wrote:
>> TypeDecorator doesn't affect the decorated type via subclassing, it affects 
>> it via the "impl" attribute, hence "decorates".   So you say self.impl = 
>> Enum(...).  You don't need to use TypeDecorator here if all you want to 
>> do is create an Enum with default arguments, just subclass Enum directly.
>> 
>> On Aug 9, 2010, at 8:24 AM, Nikolaj wrote:
>> 
>>> I'm on MySQL and trying to create a type that stores enums as
>>> VARCHAR(255). However, the following code still issues an ENUM() DDL.
>>> What am I doing wrong here?
>> 
>>> class StringEnum(types.TypeDecorator):
>>>impl = types.Enum
>> 
>>>def __init__(self, *args, **kwargs):
>>>super(StringEnum, self).__init__(*args, native_enum=False,
>>> **kwargs)
>>>self.length = 255
>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To post to this group, send email to sqlalch...@googlegroups.com.
>>> To unsubscribe from this group, send email to 
>>> sqlalchemy+unsubscr...@googlegroups.com.
>>> For more options, visit this group 
>>> athttp://groups.google.com/group/sqlalchemy?hl=en.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

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



[sqlalchemy] Re: backref list empy

2010-08-09 Thread zzer0
Hi

I thank you very much for your help. I managed to figure out that my
Inquiries table had no associated types in the actual sqlite database,
so after I changed the person_id field to Numeric, all was well and
done without changes to the code. Perhaps just to be aware of.

As for the Adjacency List Relationship problem: It worked wonderfully,
I was not able to find a solution before on Google.

Kind Regards

-- 
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: backref list empy

2010-08-09 Thread zzer0
Hi

I thank you very much for your help. I managed to figure out that my
Inquiries table had no associated types in the actual sqlite database,
so after I changed the person_id field to Numeric, all was well and
done without changes to the code. Perhaps just to be aware of.

As for the Adjacency List Relationship problem: It worked wonderfully,
I was not able to find a solution before on Google.

Kind Regards

-- 
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: Proper way to make a TypeDecorator around Enum?

2010-08-09 Thread Nikolaj
It makes sense to subclass the type directly if I'm not messing with
result or bind values. This works:

class StringEnum(types.Enum):
def __init__(self, *args, **kwargs):
super(StringEnum, self).__init__(*args, native_enum=False,
**kwargs)
self.length = 255

I'm still curious why the TypeDecorator I pasted previously doesn't
work though. The superconstructor does the self.impl =
self.__class__.impl(...) anyway so I figured this was the better
approach than calling self.impl = Enum(...).

On Aug 9, 2:56 pm, Michael Bayer  wrote:
> TypeDecorator doesn't affect the decorated type via subclassing, it affects 
> it via the "impl" attribute, hence "decorates".   So you say self.impl = 
> Enum(...).      You don't need to use TypeDecorator here if all you want to 
> do is create an Enum with default arguments, just subclass Enum directly.
>
> On Aug 9, 2010, at 8:24 AM, Nikolaj wrote:
>
> > I'm on MySQL and trying to create a type that stores enums as
> > VARCHAR(255). However, the following code still issues an ENUM() DDL.
> > What am I doing wrong here?
>
> > class StringEnum(types.TypeDecorator):
> >    impl = types.Enum
>
> >    def __init__(self, *args, **kwargs):
> >        super(StringEnum, self).__init__(*args, native_enum=False,
> > **kwargs)
> >        self.length = 255
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "sqlalchemy" group.
> > To post to this group, send email to sqlalch...@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > sqlalchemy+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/sqlalchemy?hl=en.

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



Re: [sqlalchemy] backref list empy

2010-08-09 Thread Michael Bayer

On Aug 9, 2010, at 5:13 AM, zzer0 wrote:

> Hi there.
> 
> I have 2 declarative_base classes: Person and Inquiry. I have created
> a relationship in Inquiry with a backref to Person.
> When observing the an Inquiry object the person variable correctly
> points to Person, however when observing Person objects I always get
> an empty list in inquiries variable.

The code below is fine so the origin of the issue probably lies within 
configuration or usage that is not illustrated here.   


> Also on a different topic I have not been able to figure out how to do
> Adjacency List Relationship here to backref Person to Person as you
> can see in the referred_by variable, which I commented out.

Person needs to be passed as a late-resolved string expression here, and the 
many-to-one side needs the "remote_side" argument, in this case :


referred_by = relationship("Person", remote_side=id, backref =
backref("people_referred", order_by = id))



> Any help would be appreciated greatly - thank you
> 
> class Person(Base):
>__tablename__ = "people"
> 
>id = Column(Integer, primary_key=True)
>names = Column(String)
>surname = Column(String)
>mobile_number = Column(Integer)
>land_number = Column(Integer)
>email = Column(String)
>address_id = Column(Integer,ForeignKey("addresses.id"))
>organization_id = Column(Integer,ForeignKey("organizations.id"))
>net_worth = Column(Integer)
>annual_net_income = Column(Integer)
>work_position_id =
> Column(Integer,ForeignKey("people_work_positions.id"))
>rapport = Column(Integer)
>leads_generator = Column(Integer)
>notes = Column(String)
>referred_by_id = Column(Integer,ForeignKey("people.id"))
>designation_id =
> Column(Integer,ForeignKey("people_designations.id"))
>other_contacts = Column(String)
>date_of_birth = Column(Integer)
>system_folder = Column(String)
>lead_source_id = Column(Integer,ForeignKey("leads_sources.id"))
> 
>address = relationship(Address, backref = backref("people",
> order_by = id))
>organization = relationship(Organization, backref =
> backref("people", order_by = id))
>work_position = relationship(WorkPosition, backref =
> backref("people", order_by = id))
>designation = relationship(Designation, backref =
> backref("people", order_by = id))
>lead_source = relationship(LeadSource, backref = backref("people",
> order_by = id))
>#referred_by = relationship(Person, backref =
> backref("people_referred", order_by = id))
> 
> 
> class Inquiry(Base):
>__tablename__ = "inquiries"
> 
>id = Column(Integer, primary_key=True)
>projected_close_date = Column(Date)
>closed_date = Column(Date)
>opened_date = Column(Date)
>service_start_date = Column(Date)
>process_step_id = Column(Integer,
> ForeignKey("inquiries_process_steps.id"))
>commission = Column(Integer)
>cost = Column(Integer)
>forecast = Column(Integer)#1-5 10,30,50,70,90 %
>person_id = Column(Integer, ForeignKey("people.id"))
>organization_id = Column(Integer, ForeignKey("organizations.id"))
>product_service_id = Column(Integer,
> ForeignKey("products_services.id"))
>notes = Column(String)
>lost_date = Column(Date)
> 
>process_step = relationship(ProcessStep, backref =
> backref("inquiries", order_by = id))
>person = relationship(Person, backref = backref("inquiries",
> order_by = id))
>organization = relationship(Organization, backref =
> backref("inquiries", order_by = id))
>product_service = relationship(ProductService, backref =
> backref("inquiries", order_by = id))
> 
> -- 
> 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] Proper way to make a TypeDecorator around Enum?

2010-08-09 Thread Michael Bayer
TypeDecorator doesn't affect the decorated type via subclassing, it affects it 
via the "impl" attribute, hence "decorates".   So you say self.impl = 
Enum(...).  You don't need to use TypeDecorator here if all you want to do 
is create an Enum with default arguments, just subclass Enum directly.


On Aug 9, 2010, at 8:24 AM, Nikolaj wrote:

> I'm on MySQL and trying to create a type that stores enums as
> VARCHAR(255). However, the following code still issues an ENUM() DDL.
> What am I doing wrong here?
> 
> class StringEnum(types.TypeDecorator):
>impl = types.Enum
> 
>def __init__(self, *args, **kwargs):
>super(StringEnum, self).__init__(*args, native_enum=False,
> **kwargs)
>self.length = 255
> 
> -- 
> 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] Proper way to make a TypeDecorator around Enum?

2010-08-09 Thread Nikolaj
I'm on MySQL and trying to create a type that stores enums as
VARCHAR(255). However, the following code still issues an ENUM() DDL.
What am I doing wrong here?

class StringEnum(types.TypeDecorator):
impl = types.Enum

def __init__(self, *args, **kwargs):
super(StringEnum, self).__init__(*args, native_enum=False,
**kwargs)
self.length = 255

-- 
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] backref list empy

2010-08-09 Thread zzer0
Hi there.

I have 2 declarative_base classes: Person and Inquiry. I have created
a relationship in Inquiry with a backref to Person.
When observing the an Inquiry object the person variable correctly
points to Person, however when observing Person objects I always get
an empty list in inquiries variable.
Also on a different topic I have not been able to figure out how to do
Adjacency List Relationship here to backref Person to Person as you
can see in the referred_by variable, which I commented out.
Any help would be appreciated greatly - thank you

class Person(Base):
__tablename__ = "people"

id = Column(Integer, primary_key=True)
names = Column(String)
surname = Column(String)
mobile_number = Column(Integer)
land_number = Column(Integer)
email = Column(String)
address_id = Column(Integer,ForeignKey("addresses.id"))
organization_id = Column(Integer,ForeignKey("organizations.id"))
net_worth = Column(Integer)
annual_net_income = Column(Integer)
work_position_id =
Column(Integer,ForeignKey("people_work_positions.id"))
rapport = Column(Integer)
leads_generator = Column(Integer)
notes = Column(String)
referred_by_id = Column(Integer,ForeignKey("people.id"))
designation_id =
Column(Integer,ForeignKey("people_designations.id"))
other_contacts = Column(String)
date_of_birth = Column(Integer)
system_folder = Column(String)
lead_source_id = Column(Integer,ForeignKey("leads_sources.id"))

address = relationship(Address, backref = backref("people",
order_by = id))
organization = relationship(Organization, backref =
backref("people", order_by = id))
work_position = relationship(WorkPosition, backref =
backref("people", order_by = id))
designation = relationship(Designation, backref =
backref("people", order_by = id))
lead_source = relationship(LeadSource, backref = backref("people",
order_by = id))
#referred_by = relationship(Person, backref =
backref("people_referred", order_by = id))


class Inquiry(Base):
__tablename__ = "inquiries"

id = Column(Integer, primary_key=True)
projected_close_date = Column(Date)
closed_date = Column(Date)
opened_date = Column(Date)
service_start_date = Column(Date)
process_step_id = Column(Integer,
ForeignKey("inquiries_process_steps.id"))
commission = Column(Integer)
cost = Column(Integer)
forecast = Column(Integer)#1-5 10,30,50,70,90 %
person_id = Column(Integer, ForeignKey("people.id"))
organization_id = Column(Integer, ForeignKey("organizations.id"))
product_service_id = Column(Integer,
ForeignKey("products_services.id"))
notes = Column(String)
lost_date = Column(Date)

process_step = relationship(ProcessStep, backref =
backref("inquiries", order_by = id))
person = relationship(Person, backref = backref("inquiries",
order_by = id))
organization = relationship(Organization, backref =
backref("inquiries", order_by = id))
product_service = relationship(ProductService, backref =
backref("inquiries", order_by = id))

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