[sqlalchemy] Re: moving an object
Currently, I use accountA.remove(entry) and I have rewritten insort to bypass the bug you say. So, AFAIK, whereas an entry has only one account (via entry.account_id), SA can't remove the first relation. It's dangerous, because if developer forget to remove the first relation, the entry is contained in 2 accounts temporaly. It can lead to false computation (when summing balance for instance). On 5 avr, 22:03, jason kirtland j...@discorporate.us wrote: jean-philippe dutreve wrote: Hi all, I wonder if SA can handle this use case: An Account can contain Entries ordered by 'position' attribute. mapper(Account, table_accounts, properties = dict( entries = relation(Entry, lazy=True, collection_class=ordering_list ('position'), order_by=[table_entries.c.position], passive_deletes='all', cascade='save-update', backref=backref('account', lazy=False), ), )) I'd like to move an entry from accountA to accountB and let SA remove the link between the entry and accountA: entry = accountA.entries[0] insort_right(accountB.entries, entry) assert not entry in accountA.entries# false, entry is still in accountA It is possible? Try removing the entry from accountA: entry = accountA.pop(0) ... Also beware that bisect insort has a bug that prevents it from working properly with list subclasses like ordering_list (or any SA list-based collection). I think it's fixed in Python 3.0, not sure if the fix was backported to 2.x. --~--~-~--~~~---~--~~ 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: session close - whatfor?
not at all, when you commit/rollback, everything in the session is expired (assuming you're on 0.5). 0.5.2 Doing a remove() at the end of a request is a good way to ensure nothing is around from the previous request but in theory its not needed. But again, I've no idea what TG does in this regard. Nothing anymore. I ripped the transactional middleware out replaced it with the decorator mentioned in my initial post. Well, for safety I will continue closing the session. Thanks, Diez --~--~-~--~~~---~--~~ 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: moving an object
So, we would like SA to have some kind of operation like reparent_item() that would move an object from one relation to another. It seems to me that this is is better handled as a piece of application business logic. In this case, provide a move_entry() function that properly encapsulates inserting and removing the entry in a single operation. I can imagine that there would be many variations on business rules for moving an item that would be difficult to encapsulate in a common operation within SA. -- Mike Conley On Mon, Apr 6, 2009 at 2:10 AM, jean-philippe dutreve jdutr...@gmail.comwrote: Currently, I use accountA.remove(entry) and I have rewritten insort to bypass the bug you say. So, AFAIK, whereas an entry has only one account (via entry.account_id), SA can't remove the first relation. It's dangerous, because if developer forget to remove the first relation, the entry is contained in 2 accounts temporaly. It can lead to false computation (when summing balance for instance). On 5 avr, 22:03, jason kirtland j...@discorporate.us wrote: jean-philippe dutreve wrote: Hi all, I wonder if SA can handle this use case: An Account can contain Entries ordered by 'position' attribute. mapper(Account, table_accounts, properties = dict( entries = relation(Entry, lazy=True, collection_class=ordering_list ('position'), order_by=[table_entries.c.position], passive_deletes='all', cascade='save-update', backref=backref('account', lazy=False), ), )) I'd like to move an entry from accountA to accountB and let SA remove the link between the entry and accountA: entry = accountA.entries[0] insort_right(accountB.entries, entry) assert not entry in accountA.entries# false, entry is still in accountA It is possible? Try removing the entry from accountA: entry = accountA.pop(0) ... Also beware that bisect insort has a bug that prevents it from working properly with list subclasses like ordering_list (or any SA list-based collection). I think it's fixed in Python 3.0, not sure if the fix was backported to 2.x. --~--~-~--~~~---~--~~ 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: moving an object
The object doesn't move from one relation to another : this is the same relation 'entries' but on a different parent. This is common to any parent.child pattern, not a business specific case. The current behavior is not consistent because as soon as we commit() + refresh(), the object is not on accountA anymore (entry.account_id has changed). This end result should be reflected in memory just after the change too (i.e. before commit). On Apr 6, 1:10 pm, Mike Conley mconl...@gmail.com wrote: So, we would like SA to have some kind of operation like reparent_item() that would move an object from one relation to another. It seems to me that this is is better handled as a piece of application business logic. In this case, provide a move_entry() function that properly encapsulates inserting and removing the entry in a single operation. I can imagine that there would be many variations on business rules for moving an item that would be difficult to encapsulate in a common operation within SA. -- Mike Conley On Mon, Apr 6, 2009 at 2:10 AM, jean-philippe dutreve jdutr...@gmail.comwrote: Currently, I use accountA.remove(entry) and I have rewritten insort to bypass the bug you say. So, AFAIK, whereas an entry has only one account (via entry.account_id), SA can't remove the first relation. It's dangerous, because if developer forget to remove the first relation, the entry is contained in 2 accounts temporaly. It can lead to false computation (when summing balance for instance). On 5 avr, 22:03, jason kirtland j...@discorporate.us wrote: jean-philippe dutreve wrote: Hi all, I wonder if SA can handle this use case: An Account can contain Entries ordered by 'position' attribute. mapper(Account, table_accounts, properties = dict( entries = relation(Entry, lazy=True, collection_class=ordering_list ('position'), order_by=[table_entries.c.position], passive_deletes='all', cascade='save-update', backref=backref('account', lazy=False), ), )) I'd like to move an entry from accountA to accountB and let SA remove the link between the entry and accountA: entry = accountA.entries[0] insort_right(accountB.entries, entry) assert not entry in accountA.entries # false, entry is still in accountA It is possible? Try removing the entry from accountA: entry = accountA.pop(0) ... Also beware that bisect insort has a bug that prevents it from working properly with list subclasses like ordering_list (or any SA list-based collection). I think it's fixed in Python 3.0, not sure if the fix was backported to 2.x. --~--~-~--~~~---~--~~ 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] Need some help with simple mapping
Hi, I have tried all kinds of tutorials, but I cant get two simple tables mapped. Here are the tables: class User(object): def __init__(self,username,password): self.username = username self.password = password class Email(object): def __init__(self, address, user): self.address = address self.user = user All I wish to accomplish is the following: tom_user = User(tom,passy) tom_email = Email(t...@someemail.com, tom_user) session.add(tom_user) session.add(tom_email) session.commit() But this doesnt work, can someone flesh out the tables and the mapping for me so I know what I am doing wrong. Thanks Kashif --~--~-~--~~~---~--~~ 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: moving an object
im slightly confused. the backref should be automatically reparenting, not sure if ordering_list interferes with that, but in any case after you flush()/expire() or commit(), it will definitely happen since all collections will load fresh. Mike Conley wrote: So, we would like SA to have some kind of operation like reparent_item() that would move an object from one relation to another. It seems to me that this is is better handled as a piece of application business logic. In this case, provide a move_entry() function that properly encapsulates inserting and removing the entry in a single operation. I can imagine that there would be many variations on business rules for moving an item that would be difficult to encapsulate in a common operation within SA. -- Mike Conley On Mon, Apr 6, 2009 at 2:10 AM, jean-philippe dutreve jdutr...@gmail.comwrote: Currently, I use accountA.remove(entry) and I have rewritten insort to bypass the bug you say. So, AFAIK, whereas an entry has only one account (via entry.account_id), SA can't remove the first relation. It's dangerous, because if developer forget to remove the first relation, the entry is contained in 2 accounts temporaly. It can lead to false computation (when summing balance for instance). On 5 avr, 22:03, jason kirtland j...@discorporate.us wrote: jean-philippe dutreve wrote: Hi all, I wonder if SA can handle this use case: An Account can contain Entries ordered by 'position' attribute. mapper(Account, table_accounts, properties = dict( entries = relation(Entry, lazy=True, collection_class=ordering_list ('position'), order_by=[table_entries.c.position], passive_deletes='all', cascade='save-update', backref=backref('account', lazy=False), ), )) I'd like to move an entry from accountA to accountB and let SA remove the link between the entry and accountA: entry = accountA.entries[0] insort_right(accountB.entries, entry) assert not entry in accountA.entries# false, entry is still in accountA It is possible? Try removing the entry from accountA: entry = accountA.pop(0) ... Also beware that bisect insort has a bug that prevents it from working properly with list subclasses like ordering_list (or any SA list-based collection). I think it's fixed in Python 3.0, not sure if the fix was backported to 2.x. --~--~-~--~~~---~--~~ 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: Need some help with simple mapping
Kashif wrote: Hi, I have tried all kinds of tutorials, but I cant get two simple tables mapped. pretty much this exact example is present in *the* tutorial which is the ORM tutorial at http://www.sqlalchemy.org/docs/ . Here are the tables: class User(object): def __init__(self,username,password): self.username = username self.password = password class Email(object): def __init__(self, address, user): self.address = address self.user = user All I wish to accomplish is the following: tom_user = User(tom,passy) tom_email = Email(t...@someemail.com, tom_user) session.add(tom_user) session.add(tom_email) session.commit() But this doesnt work, can someone flesh out the tables and the mapping for me so I know what I am doing wrong. Thanks Kashif --~--~-~--~~~---~--~~ 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: moving an object
It would be fine/safe that accountA has entry removed BEFORE any reload (with explicit refresh/expire/commit). I can't remember, but a previous version of SA had this behavior. On Apr 6, 4:42 pm, Michael Bayer mike...@zzzcomputing.com wrote: im slightly confused. the backref should be automatically reparenting, not sure if ordering_list interferes with that, but in any case after you flush()/expire() or commit(), it will definitely happen since all collections will load fresh. Mike Conley wrote: So, we would like SA to have some kind of operation like reparent_item() that would move an object from one relation to another. It seems to me that this is is better handled as a piece of application business logic. In this case, provide a move_entry() function that properly encapsulates inserting and removing the entry in a single operation. I can imagine that there would be many variations on business rules for moving an item that would be difficult to encapsulate in a common operation within SA. -- Mike Conley On Mon, Apr 6, 2009 at 2:10 AM, jean-philippe dutreve jdutr...@gmail.comwrote: Currently, I use accountA.remove(entry) and I have rewritten insort to bypass the bug you say. So, AFAIK, whereas an entry has only one account (via entry.account_id), SA can't remove the first relation. It's dangerous, because if developer forget to remove the first relation, the entry is contained in 2 accounts temporaly. It can lead to false computation (when summing balance for instance). On 5 avr, 22:03, jason kirtland j...@discorporate.us wrote: jean-philippe dutreve wrote: Hi all, I wonder if SA can handle this use case: An Account can contain Entries ordered by 'position' attribute. mapper(Account, table_accounts, properties = dict( entries = relation(Entry, lazy=True, collection_class=ordering_list ('position'), order_by=[table_entries.c.position], passive_deletes='all', cascade='save-update', backref=backref('account', lazy=False), ), )) I'd like to move an entry from accountA to accountB and let SA remove the link between the entry and accountA: entry = accountA.entries[0] insort_right(accountB.entries, entry) assert not entry in accountA.entries # false, entry is still in accountA It is possible? Try removing the entry from accountA: entry = accountA.pop(0) ... Also beware that bisect insort has a bug that prevents it from working properly with list subclasses like ordering_list (or any SA list-based collection). I think it's fixed in Python 3.0, not sure if the fix was backported to 2.x. --~--~-~--~~~---~--~~ 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: Need some help with simple mapping
Tried that already, I get an error: InterfaceError: (InterfaceError) Error binding parameter 1 - probably unsupported type. u 'INSERT INTO emails (address, user) VALUES (?, ?)' ['e...@ped.com', __main__.User object at 0x92edcec] or I get a ProgrammingError that says something like 'cant adapt...' PS: I am interested in the non-declarative style On Apr 6, 7:44 pm, Michael Bayer mike...@zzzcomputing.com wrote: Kashif wrote: Hi, I have tried all kinds of tutorials, but I cant get two simple tables mapped. pretty much this exact example is present in *the* tutorial which is the ORM tutorial athttp://www.sqlalchemy.org/docs/. Here are the tables: class User(object): def __init__(self,username,password): self.username = username self.password = password class Email(object): def __init__(self, address, user): self.address = address self.user = user All I wish to accomplish is the following: tom_user = User(tom,passy) tom_email = Email(t...@someemail.com, tom_user) session.add(tom_user) session.add(tom_email) session.commit() But this doesnt work, can someone flesh out the tables and the mapping for me so I know what I am doing wrong. Thanks Kashif --~--~-~--~~~---~--~~ 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: moving an object
OK in fact this can possibly be implemented if the initiator passed during attribute mutation operations consisted of not just an AttributeImpl but also a target instance, so that append/remove/set operations can have the information they need continue down the chain of events without exiting prematurely. Such as this test below: from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) stuff = relation(Stuff, backref=parent) class Stuff(Base): __tablename__ = 'stuff' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parent.id')) p1 = Parent() p2 = Parent() s1 = Stuff() p1.stuff.append(s1) p2.stuff.append(s1) assert s1.parent is p2 assert s1 not in p1.stuff assert s1 in p2.stuff can be made to pass if we say this: Index: lib/sqlalchemy/orm/attributes.py === --- lib/sqlalchemy/orm/attributes.py(revision 5901) +++ lib/sqlalchemy/orm/attributes.py(working copy) @@ -679,9 +679,6 @@ collection.append_with_event(value, initiator) def remove(self, state, value, initiator, passive=PASSIVE_OFF): -if initiator is self: -return - collection = self.get_collection(state, passive=passive) if collection is PASSIVE_NORESULT: self.fire_remove_event(state, value, initiator) so some more complete way of not exiting the event loop too soon would need to be implemented. Jason, any comments on this ? jean-philippe dutreve wrote: It would be fine/safe that accountA has entry removed BEFORE any reload (with explicit refresh/expire/commit). I can't remember, but a previous version of SA had this behavior. On Apr 6, 4:42 pm, Michael Bayer mike...@zzzcomputing.com wrote: im slightly confused. the backref should be automatically reparenting, not sure if ordering_list interferes with that, but in any case after you flush()/expire() or commit(), it will definitely happen since all collections will load fresh. Mike Conley wrote: So, we would like SA to have some kind of operation like reparent_item() that would move an object from one relation to another. It seems to me that this is is better handled as a piece of application business logic. In this case, provide a move_entry() function that properly encapsulates inserting and removing the entry in a single operation. I can imagine that there would be many variations on business rules for moving an item that would be difficult to encapsulate in a common operation within SA. -- Mike Conley On Mon, Apr 6, 2009 at 2:10 AM, jean-philippe dutreve jdutr...@gmail.comwrote: Currently, I use accountA.remove(entry) and I have rewritten insort to bypass the bug you say. So, AFAIK, whereas an entry has only one account (via entry.account_id), SA can't remove the first relation. It's dangerous, because if developer forget to remove the first relation, the entry is contained in 2 accounts temporaly. It can lead to false computation (when summing balance for instance). On 5 avr, 22:03, jason kirtland j...@discorporate.us wrote: jean-philippe dutreve wrote: Hi all, I wonder if SA can handle this use case: An Account can contain Entries ordered by 'position' attribute. mapper(Account, table_accounts, properties = dict( entries = relation(Entry, lazy=True, collection_class=ordering_list ('position'), order_by=[table_entries.c.position], passive_deletes='all', cascade='save-update', backref=backref('account', lazy=False), ), )) I'd like to move an entry from accountA to accountB and let SA remove the link between the entry and accountA: entry = accountA.entries[0] insort_right(accountB.entries, entry) assert not entry in accountA.entries # false, entry is still in accountA It is possible? Try removing the entry from accountA: entry = accountA.pop(0) ... Also beware that bisect insort has a bug that prevents it from working properly with list subclasses like ordering_list (or any SA list-based collection). I think it's fixed in Python 3.0, not sure if the fix was backported to 2.x. --~--~-~--~~~---~--~~ 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: Need some help with simple mapping
Kashif wrote: Tried that already, I get an error: InterfaceError: (InterfaceError) Error binding parameter 1 - probably unsupported type. u 'INSERT INTO emails (address, user) VALUES (?, ?)' ['e...@ped.com', __main__.User object at 0x92edcec] or I get a ProgrammingError that says something like 'cant adapt...' PS: I am interested in the non-declarative style if you'd like to store a User object on an attribute named user, then you can't map that attribute to a column named user. you need to construct a foreign key on your table. please read: http://www.sqlalchemy.org/docs/05/mappers.html#many-to-one On Apr 6, 7:44 pm, Michael Bayer mike...@zzzcomputing.com wrote: Kashif wrote: Hi, I have tried all kinds of tutorials, but I cant get two simple tables mapped. pretty much this exact example is present in *the* tutorial which is the ORM tutorial athttp://www.sqlalchemy.org/docs/. Here are the tables: class User(object): def __init__(self,username,password): self.username = username self.password = password class Email(object): def __init__(self, address, user): self.address = address self.user = user All I wish to accomplish is the following: tom_user = User(tom,passy) tom_email = Email(t...@someemail.com, tom_user) session.add(tom_user) session.add(tom_email) session.commit() But this doesnt work, can someone flesh out the tables and the mapping for me so I know what I am doing wrong. Thanks Kashif --~--~-~--~~~---~--~~ 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] Complicated query
Hello everyone, I've got this query: selectexpr = session.query(Host, Architecture, OS_Kind, OS_version, Virtualization, Project, Reservation) ... selectexpr = selectexpr.filter(or_(Host.earliest_reservation_id == None, and_(Host.earliest_reservation_id == Reservation.id, Reservation.start_date sd))) The problem with this query is that SQLA/Postgres does a join on Host.earliest_reservation_id == None (producing each combination of every Reservation and every host with Host.earliest_reservation_id = NULL). Now I could make a second query like session.query(Host, Architecture, OS_Kind, OS_version, Virtualization, Project), but do I have to? Is there some way to account for condition Host.earliest_reservation_id == None and produce None / NULL as Reservation in resulting row tuple as result of a single query? Regards, mk --~--~-~--~~~---~--~~ 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: Need some help with simple mapping
Yes, I did that and that is what causes problems, I am attaching the actual mapping underneath. Note the email object has a user object. Any thoughts? user_table = sa.Table('user', meta.metadata, sa.Column('id', types.String(50), primary_key=True), sa.Column('username', types.String(255)), sa.Column('password', types.String(255)), ) email_table = sa.Table('email', meta.metadata, sa.Column('user', types.String(50), sa.ForeignKey('user.id')), sa.Column('email', types.String(50), primary_key=True), ) Thanks Kashif On Apr 6, 8:18 pm, Michael Bayer mike...@zzzcomputing.com wrote: Kashif wrote: Tried that already, I get an error: InterfaceError: (InterfaceError) Error binding parameter 1 - probably unsupported type. u 'INSERT INTO emails (address, user) VALUES (?, ?)' ['@ped.com', __main__.User object at 0x92edcec] or I get a ProgrammingError that says something like 'cant adapt...' PS: I am interested in the non-declarative style if you'd like to store a User object on an attribute named user, then you can't map that attribute to a column named user. you need to construct a foreign key on your table. please read: http://www.sqlalchemy.org/docs/05/mappers.html#many-to-one On Apr 6, 7:44 pm, Michael Bayer mike...@zzzcomputing.com wrote: Kashif wrote: Hi, I have tried all kinds of tutorials, but I cant get two simple tables mapped. pretty much this exact example is present in *the* tutorial which is the ORM tutorial athttp://www.sqlalchemy.org/docs/. Here are the tables: class User(object): def __init__(self,username,password): self.username = username self.password = password class Email(object): def __init__(self, address, user): self.address = address self.user = user All I wish to accomplish is the following: tom_user = User(tom,passy) tom_email = Email(t...@someemail.com, tom_user) session.add(tom_user) session.add(tom_email) session.commit() But this doesnt work, can someone flesh out the tables and the mapping for me so I know what I am doing wrong. Thanks Kashif --~--~-~--~~~---~--~~ 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] Self-referential with DeclarativeBase
Hi, I'm trying to migrate my TurboGears project from SQLObject to SQLAlchemy. As the TurboGears2 way of using SQLAlchemy is the DeclarativeBase, I'm trying to achive a self-referential using the DeclarativeBase. Following the documentation, it seems that the following code could be correct (after a translation from the Base example), but in fact, it complains that the Country name is not defined... File /is2dev/dev/model/model.py, line 47, in Country parent = relation(Country, backref=backref('child'), cascade=all) NameError: name 'Country' is not defined I just try to make a tree with the world continents regions countries. class Country(DeclarativeBase): Country with iso and related region __tablename__ = 'countries' #{ Columns id = Column(Integer, primary_key=True) name = Column(Unicode(100), nullable=False) iso = Column(Unicode(3), nullable=False) created = Column(DateTime(), default=datetime.now(), nullable=False) updated = Column(DateTime(), nullable=True) #{ Relations parent_id = Column(Integer, ForeignKey('countries.id'), nullable=True) parent = relation(Country, backref=backref('child'), cascade=all) #{ Special methods #} Thanks for your help... Regards Cédric --~--~-~--~~~---~--~~ 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: Complicated query
On Monday 06 April 2009 18:26:06 Marcin Krol wrote: Hello everyone, I've got this query: selectexpr = session.query(Host, Architecture, OS_Kind, OS_version, Virtualization, Project, Reservation) ... selectexpr = selectexpr.filter(or_(Host.earliest_reservation_id == None, and_(Host.earliest_reservation_id == Reservation.id, Reservation.start_date sd))) The problem with this query is that SQLA/Postgres does a join on Host.earliest_reservation_id == None (producing each combination of every Reservation and every host with Host.earliest_reservation_id = NULL). Now I could make a second query like session.query(Host, Architecture, OS_Kind, OS_version, Virtualization, Project), but do I have to? Is there some way to account for condition Host.earliest_reservation_id == None and produce None / NULL as Reservation in resulting row tuple as result of a single query? left outer join? --~--~-~--~~~---~--~~ 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: Self-referential with DeclarativeBase
Hi, As you are defining Country when you use it within your relation, you need to define your relation like this : parent = relation(Country, backref=backref('child'), cascade=all) Note the quotes around Country. Regards, Laurent Cedric a écrit : Hi, I'm trying to migrate my TurboGears project from SQLObject to SQLAlchemy. As the TurboGears2 way of using SQLAlchemy is the DeclarativeBase, I'm trying to achive a self-referential using the DeclarativeBase. Following the documentation, it seems that the following code could be correct (after a translation from the Base example), but in fact, it complains that the Country name is not defined... File /is2dev/dev/model/model.py, line 47, in Country parent = relation(Country, backref=backref('child'), cascade=all) NameError: name 'Country' is not defined I just try to make a tree with the world continents regions countries. class Country(DeclarativeBase): Country with iso and related region __tablename__ = 'countries' #{ Columns id = Column(Integer, primary_key=True) name = Column(Unicode(100), nullable=False) iso = Column(Unicode(3), nullable=False) created = Column(DateTime(), default=datetime.now(), nullable=False) updated = Column(DateTime(), nullable=True) #{ Relations parent_id = Column(Integer, ForeignKey('countries.id'), nullable=True) parent = relation(Country, backref=backref('child'), cascade=all) #{ Special methods #} Thanks for your help... Regards Cédric --~--~-~--~~~---~--~~ 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: Need some help with simple mapping
map the user column to another name, since your user relation is blocking it. Kashif wrote: Yes, I did that and that is what causes problems, I am attaching the actual mapping underneath. Note the email object has a user object. Any thoughts? user_table = sa.Table('user', meta.metadata, sa.Column('id', types.String(50), primary_key=True), sa.Column('username', types.String(255)), sa.Column('password', types.String(255)), ) email_table = sa.Table('email', meta.metadata, sa.Column('user', types.String(50), sa.ForeignKey('user.id')), sa.Column('email', types.String(50), primary_key=True), ) Thanks Kashif On Apr 6, 8:18 pm, Michael Bayer mike...@zzzcomputing.com wrote: Kashif wrote: Tried that already, I get an error: InterfaceError: (InterfaceError) Error binding parameter 1 - probably unsupported type. u 'INSERT INTO emails (address, user) VALUES (?, ?)' ['@ped.com', __main__.User object at 0x92edcec] or I get a ProgrammingError that says something like 'cant adapt...' PS: I am interested in the non-declarative style if you'd like to store a User object on an attribute named user, then you can't map that attribute to a column named user. you need to construct a foreign key on your table. please read: http://www.sqlalchemy.org/docs/05/mappers.html#many-to-one On Apr 6, 7:44 pm, Michael Bayer mike...@zzzcomputing.com wrote: Kashif wrote: Hi, I have tried all kinds of tutorials, but I cant get two simple tables mapped. pretty much this exact example is present in *the* tutorial which is the ORM tutorial athttp://www.sqlalchemy.org/docs/. Here are the tables: class User(object): def __init__(self,username,password): self.username = username self.password = password class Email(object): def __init__(self, address, user): self.address = address self.user = user All I wish to accomplish is the following: tom_user = User(tom,passy) tom_email = Email(t...@someemail.com, tom_user) session.add(tom_user) session.add(tom_email) session.commit() But this doesnt work, can someone flesh out the tables and the mapping for me so I know what I am doing wrong. Thanks Kashif --~--~-~--~~~---~--~~ 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: Self-referential with DeclarativeBase
Thanks Laurent, It solves the problem! Best Regards, Cédric On Apr 6, 6:23 pm, Laurent Rahuel laurent.rah...@gmail.com wrote: Hi, As you are defining Country when you use it within your relation, you need to define your relation like this : parent = relation(Country, backref=backref('child'), cascade=all) Note the quotes around Country. Regards, Laurent Cedric a écrit : Hi, I'm trying to migrate my TurboGears project from SQLObject to SQLAlchemy. As the TurboGears2 way of using SQLAlchemy is the DeclarativeBase, I'm trying to achive a self-referential using the DeclarativeBase. Following the documentation, it seems that the following code could be correct (after a translation from the Base example), but in fact, it complains that the Country name is not defined... File /is2dev/dev/model/model.py, line 47, in Country parent = relation(Country, backref=backref('child'), cascade=all) NameError: name 'Country' is not defined I just try to make a tree with the world continents regions countries. class Country(DeclarativeBase): Country with iso and related region __tablename__ = 'countries' #{ Columns id = Column(Integer, primary_key=True) name = Column(Unicode(100), nullable=False) iso = Column(Unicode(3), nullable=False) created = Column(DateTime(), default=datetime.now(), nullable=False) updated = Column(DateTime(), nullable=True) #{ Relations parent_id = Column(Integer, ForeignKey('countries.id'), nullable=True) parent = relation(Country, backref=backref('child'), cascade=all) #{ Special methods #} Thanks for your help... Regards Cédric --~--~-~--~~~---~--~~ 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: Supporting fully-qualified table names and cross-database references in Sybase and SQL Server?
I uploaded a patch to trac On Mar 13, 12:51 pm, phrrn...@googlemail.com phrrn...@googlemail.com wrote: opened ticket 1341 for this. http://www.sqlalchemy.org/trac/ticket/1341 --~--~-~--~~~---~--~~ 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: Supporting fully-qualified table names and cross-database references in Sybase and SQL Server?
thanks. we're all underwater with our day jobs after Pycon so will try to work through the backlog in the coming weeks. phrrn...@googlemail.com wrote: I uploaded a patch to trac On Mar 13, 12:51 pm, phrrn...@googlemail.com phrrn...@googlemail.com wrote: opened ticket 1341 for this. http://www.sqlalchemy.org/trac/ticket/1341 --~--~-~--~~~---~--~~ 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: accepts_scalar_loader attribute of AttributeImpl() object - BUG?
I think its just a flag on the class. you can probably monkeypatch it to get your app working for now (and it would be False). That works fine, many thanks. (for the archive, here's what I'm doing right after the initial sqlalchemy import:) # remove for SQLA 0.5.3+ from sqlalchemy.orm.dynamic import DynamicAttributeImpl DynamicAttributeImpl.accepts_scalar_loader = False --~--~-~--~~~---~--~~ 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] select A join B where B is null
I am trying to get all of the rows in table A that do not have a match in table B. I believe the problem is that I am using a text foreign key, and for the rows I am looking for the field will still have a value, it just won't match anything in table B. To make things confusing the table 'qstatus' is mapped to a class called 'JobInfo', also the qstatus table is a temporary table populated with values of what is currently running, so there are no foreign keys or indexes on the table, the qstat_table.c.job_number field is unique but isn't a primary key as far as PostgreSQL is concerned. This is what I am trying to do: SELECT farm.qstatus.* FROM farm.qstatus LEFT OUTER JOIN farm.job ON qstatus.job_name=job.name WHERE job IS NULL ORDER BY qstatus.job_name; This is what my mapper looks like: mapper(JobInfo, qstat_table, primary_key=[qstat_table.c.job_number], properties={ 'Chunk':relation(Chunk, lazy=True, uselist=False), 'Job' : relation(Job, primaryjoin=(qstat_table.c.job_name==job_table.c.name), foreign_keys=[qstat_table.c.job_name], lazy=True, viewonly=True, uselist=False) }, save_on_init=False) This is my python code: from farmdb import * #where my mappers live session=create_session() db.echo=True session.query(JobInfo).outerjoin(JobInfo.Job).filter(JobInfo.Job==None).order_by(JobInfo.job_name).all() 2009-04-06 16:49:26,922 INFO sqlalchemy.engine.base.Engine.0x...0210 SELECT farm.qstatus.state AS farm_qstatus_state, farm.qstatus.job_name AS farm_qstatus_job_name, farm.qstatus.priority AS farm_qstatus_priority, farm.qstatus.job_number AS farm_qstatus_job_number, farm.qstatus.owner AS farm_qstatus_owner, farm.qstatus.start_time AS farm_qstatus_start_time, farm.qstatus.queue AS farm_qstatus_queue, farm.qstatus.slots AS farm_qstatus_slots, farm.qstatus.req_que AS farm_qstatus_req_que, farm.qstatus.s_rt AS farm_qstatus_s_rt, farm.qstatus.h_rt AS farm_qstatus_h_rt FROM farm.qstatus LEFT OUTER JOIN farm.job ON farm.qstatus.job_name = farm.job.name WHERE farm.qstatus.job_name IS NULL ORDER BY farm.qstatus.job_name 2009-04-06 16:49:26,922 INFO sqlalchemy.engine.base.Engine.0x...0210 {} [] -- David Gardner Pipeline Tools Programmer, Sid the Science Kid Jim Henson Creature Shop dgard...@creatureshop.com --~--~-~--~~~---~--~~ 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: select A join B where B is null
On Apr 6, 2009, at 8:11 PM, David Gardner wrote: I am trying to get all of the rows in table A that do not have a match in table B. I believe the problem is that I am using a text foreign key, and for the rows I am looking for the field will still have a value, it just won't match anything in table B. To make things confusing the table 'qstatus' is mapped to a class called 'JobInfo', also the qstatus table is a temporary table populated with values of what is currently running, so there are no foreign keys or indexes on the table, the qstat_table.c.job_number field is unique but isn't a primary key as far as PostgreSQL is concerned. This is what I am trying to do: SELECT farm.qstatus.* FROM farm.qstatus LEFT OUTER JOIN farm.job ON qstatus.job_name=job.name WHERE job IS NULL ORDER BY qstatus.job_name; this is usually easiest via NOT EXISTS select * from table where not exists (select 1 from othertable where othertable.foo=table.bar) mapper(JobInfo, qstat_table, primary_key=[qstat_table.c.job_number], properties={ 'Chunk':relation(Chunk, lazy=True, uselist=False), 'Job' : relation(Job, primaryjoin=(qstat_table.c.job_name==job_table.c.name), foreign_keys=[qstat_table.c.job_name], lazy=True, viewonly=True, uselist=False) }, save_on_init=False) so a query like this would do it: session.query(JobInfo).filter(~JobInfo.Job.has()) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---