[sqlalchemy] Automatically execute a procedure on saving/loading from Sql database with SQLAlchemy (and MeGrok)
Hello everyone. First of all, thank you for reading this (no matter whether you can/want to help me or not) :-) Second, the question: I am using sqlalchemy under MeGrok (http://pypi.python.org/pypi/megrok.rdb) to have my Python/Grok classes stored over a RDBMS (MySql) database. I have a Python class in which one of the fields is a list of strings. I don't really find worthy to create a table to relate the class with the fields (is just a list that can take certain names of days of the week) so I was planning to store them in MySql as an string where the values would be separated with a comma (or semicolon). On the other hand, is very useful to have that field as a list (in Python classes) so here's my question: Is there a way to automatically execute an stored procedure (SQL preferably, but I could also do it on the Python side) so when the class is saved, that list field will be automatically joined (with whatever separator character) and when the class (or that field) is loaded, it will be automatically split-ed (so it will come back as a list)? In my brain, I have dreamed about something like an special type of * sqlalchemy.Column* in which you can specify something like *on_save = execute this()* and *on_load = execute that()*... :-) I also asked this very same question in the Grok-dev mail list. They suggested me the use of decorators, which I find an interesting idea, but I don't really know where to put a decorator to ensure that when that field is saved, it's saved as an array, and when it's loaded, it's loaded as a list. Oh, and, for the record, I am a newbie with this Grok over MySql thing so it may not make any sense what I just asked but... I had to try. Thank you 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.
[sqlalchemy] Re: Automatically execute a procedure on saving/loading from Sql database with SQLAlchemy (and MeGrok)
Wow... I just saw this... http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/types.html#sqlalchemy.types.TypeDecorator Maybe that's what I need! I'll confirm (for future questions) :-) 2010/10/25 Hector Blanco white.li...@gmail.com Hello everyone. First of all, thank you for reading this (no matter whether you can/want to help me or not) :-) Second, the question: I am using sqlalchemy under MeGrok (http://pypi.python.org/pypi/megrok.rdb) to have my Python/Grok classes stored over a RDBMS (MySql) database. I have a Python class in which one of the fields is a list of strings. I don't really find worthy to create a table to relate the class with the fields (is just a list that can take certain names of days of the week) so I was planning to store them in MySql as an string where the values would be separated with a comma (or semicolon). On the other hand, is very useful to have that field as a list (in Python classes) so here's my question: Is there a way to automatically execute an stored procedure (SQL preferably, but I could also do it on the Python side) so when the class is saved, that list field will be automatically joined (with whatever separator character) and when the class (or that field) is loaded, it will be automatically split-ed (so it will come back as a list)? In my brain, I have dreamed about something like an special type of * sqlalchemy.Column* in which you can specify something like *on_save = execute this()* and *on_load = execute that()*... :-) I also asked this very same question in the Grok-dev mail list. They suggested me the use of decorators, which I find an interesting idea, but I don't really know where to put a decorator to ensure that when that field is saved, it's saved as an array, and when it's loaded, it's loaded as a list. Oh, and, for the record, I am a newbie with this Grok over MySql thing so it may not make any sense what I just asked but... I had to try. Thank you 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.
[sqlalchemy] Recursive problem
Hey guys, I have a problem when I import classes from one to another. I have those classes in different modules: crm.py from CRMContactInformation import CRMContactInformation class CRM(rdb.Model): Set up crm table in the database rdb.metadata(metadata) rdb.tablename(crms) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) screen_id = Column(screen_id, Integer, ) contactInformation = relationship(CRMContactInformation, userlist=False, backref=crms) CRMContactInformation.py from CRM import CRM class CRMContactInformation(rdb.Model): Set up crm contact information table in the database rdb.metadata(metadata) rdb.tablename(crm_contact_informations) id = Column(id, Integer, ForeignKey(CRM.id), primary_key=True) owner = Column(owner, String(50)) . As you can see, I have a recursive problem because I import CRMContactInformation in CRM and CRM in CRMContactInformation. I got this error or similar: “AttributeError: ‘module’ object has no attribute ” I tried to change the imports importing the whole path. It didn't work out either. Is there any way I can use the metadata object to access the attributes of the tables? or another way to solve this? 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.
[sqlalchemy] Re: Automatically execute a procedure on saving/loading from Sql database with SQLAlchemy (and MeGrok)
Yuuup... It works like a charm! I have created a simple class that gets a dictionary and serializes its values. The underlying database is MySql... Just in case my code may help someone (or if someone has any suggestions...) here it goes: from sqlalchemy import types import logging log = logging.getLogger(__name__) class SimpleDict(types.TypeDecorator): impl = types.String size = -1 __separatorChar = chr(0x1D) __boolPrefix = b_ __intPrefix = i_ __floatPrefix = f_ __nullPrefix = n_ __specialPrefixes = set([__boolPrefix, __intPrefix, __floatPrefix, __nullPrefix]) __nullValues = set([null, None]) def __init__(self, length = 1024): self.size = int(length) super(ZepSimpleDict, self).__init__(self.size) def __toString(self, value): retval = None if isinstance(value, bool): retval = self.__boolPrefix + str(value) elif isinstance(value, float): retval = self.__floatPrefix + str(value) elif isinstance(value, int): retval = self.__intPrefix + str(value) elif (value is None) or (value in self.__nullValues): retval = self.__nullPrefix + str(None) else: retval = str(value) return retval def __fromString(self, value): retval = None prefix = None actualValue = None if len(value) 2: prefix = value[0:2] if (prefix in self.__specialPrefixes): actualValue = value[2:] if prefix == self.__boolPrefix: if actualValue == True: retval = True elif actualValue == False: retval = False else: retval = value elif prefix == self.__floatPrefix: try: retval = float(actualValue) except ValueError: retval = value elif prefix == self.__intPrefix: try: retval = int(actualValue) except ValueError: retval = value elif prefix == self.__nullPrefix: if actualValue == str(None): retval = None else: retval = value else: retval = value else: retval = value return retval def process_bind_param(self, value, dialect): value_tmp = None flattenedValue = list() retval = None if isinstance(value, dict): value_tmp = dict() for key, val in value.iteritems(): value_tmp[self.__toString(key)] = self.__toString(val) else: value_tmp = None if (value_tmp is not None): for key, val in value_tmp.iteritems(): flattenedValue.append(key) flattenedValue.append(val) retval = self.__separatorChar.join(flattenedValue) else: retval = None return retval def process_result_value(self, value, dialect): retval = dict() value_tmp = value.split(self.__separatorChar) if (len(value_tmp) 0): if (len(value_tmp) % 2 != 0): log.warn(process_result_value Processing an string with odd number of elements. This should not have happened.) for i in range(0, len(value_tmp), 2): retval[self.__fromString(value_tmp[i])] = self.__fromString(value_tmp[i+1]) return retval In my previous message, I said: *In my brain, I have dreamed about something like an special type of sqlalchemy.Column in which you can specify something like on_save = execute this() and on_load = execute that()... * This does exactly that! :) -- 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: SQLAlchemy not updating all expected columns after mutable attribute modification
I am running into a issue where it looks like SQLAlchemy is not performing the proper DB update when committing a session after modifying an object. This happens only when all references to the updated object are lost, and I update a mutable attribute BEFORE updating another attribute. It seems as if MutableAttrInstanceState.__resurrect is not properly resurrecting the object state. I have code that basically looks like this: def update_my_model(session, my_model_id): my_model = session.query(MyModel, id=my_model_id).one() my_model.mutable_attribute.mutate() my_model.normal_attribute = 42 return my_model session = self.logic.session_maker() update_my_model(session, my_model_id) session.commit() The above code does issues the SQL to update mutable_attribute, but not normal_attribute. Everything works if I move the mutable_attribute change to after the normal_attribute change, remove it completely, or assign the return value of the update_my_model call to a variable. I have been able to determine that in that case that fails (and only in that case) MutableAttrInstanceState._cleanup is being called as update_my_modelreturns. However, during the session.commit(), the call to self.manager.new_instance(state=self) in MutableAttrInstanceState.__resurrect is not returning an object that has normal_attribute set. From what I can tell the MutableAttrInstanceState instance should know about the update to normal_attribute ( InstanceState.modified_event is being called when it is set). But I'm not sure of the inner-workings of MutableAttrInstanceState,so I haven't been able to confirm this. My mutable_attribute is rather complex, so it is entirely possible that it is behaving badly in some why. However, this seems like a strange failure mode for an issue with that attribute? Anyone have ideas on what is going on here, or what my next steps should be to track it down? If any information I have not provided would be helpful, please let me know. Thanks for any help! -Lenza -- 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: Automatically execute a procedure on saving/loading from Sql database with SQLAlchemy (and MeGrok)
I hate when the identation gets messed up... Gonna try again: Yuuup... It works like a charm! I have created a simple class that gets a dictionary and serializes its values. The underlying database is MySql... Just in case my code may help someone (or if someone has any suggestions...) here it goes: from sqlalchemy import types import logging log = logging.getLogger(__name__) class ZepSimpleDict(types.TypeDecorator): impl = types.String size = -1 __separatorChar = chr(0x1D) __boolPrefix = b_ __intPrefix = i_ __floatPrefix = f_ __nullPrefix = n_ __specialPrefixes = set([__boolPrefix, __intPrefix, __floatPrefix, __nullPrefix]) __nullValues = set([null, None]) def __init__(self, length = 1024): self.size = int(length) super(ZepSimpleDict, self).__init__(self.size) def __toString(self, value): retval = None if isinstance(value, bool): retval = self.__boolPrefix + str(value) elif isinstance(value, float): retval = self.__floatPrefix + str(value) elif isinstance(value, int): retval = self.__intPrefix + str(value) elif (value is None) or (value in self.__nullValues): retval = self.__nullPrefix + str(None) else: retval = str(value) return retval def __fromString(self, value): retval = None prefix = None actualValue = None if len(value) 2: prefix = value[0:2] if (prefix in self.__specialPrefixes): actualValue = value[2:] if prefix == self.__boolPrefix: if actualValue == True: retval = True elif actualValue == False: retval = False else: retval = value elif prefix == self.__floatPrefix: try: retval = float(actualValue) except ValueError: retval = value elif prefix == self.__intPrefix: try: retval = int(actualValue) except ValueError: retval = value elif prefix == self.__nullPrefix: if actualValue == str(None): retval = None else: retval = value else: retval = value else: retval = value return retval def process_bind_param(self, value, dialect): value_tmp = None flattenedValue = list() retval = None if isinstance(value, dict): value_tmp = dict() for key, val in value.iteritems(): value_tmp[self.__toString(key)] = self.__toString(val) else: value_tmp = None if (value_tmp is not None): for key, val in value_tmp.iteritems(): flattenedValue.append(key) flattenedValue.append(val) retval = self.__separatorChar.join(flattenedValue) else: retval = None return retval def process_result_value(self, value, dialect): retval = dict() value_tmp = value.split(self.__separatorChar) if (len(value_tmp) 0): if (len(value_tmp) % 2 != 0): log.warn(process_result_value Processing an string with odd number of elements. This should not have happened.) for i in range(0, len(value_tmp), 2): retval[self.__fromString(value_tmp[i])] = self.__fromString(value_tmp[i+1]) return retval
Re: [sqlalchemy] Re: SQLAlchemy not updating all expected columns after mutable attribute modification
On Oct 25, 2010, at 1:07 PM, Lenza McElrath wrote: I am running into a issue where it looks like SQLAlchemy is not performing the proper DB update when committing a session after modifying an object. This happens only when all references to the updated object are lost, and I update a mutable attribute BEFORE updating another attribute. It seems as if MutableAttrInstanceState.__resurrect is not properly resurrecting the object state. I have code that basically looks like this: def update_my_model(session, my_model_id): my_model = session.query(MyModel, id=my_model_id).one() my_model.mutable_attribute.mutate() my_model.normal_attribute = 42 return my_model session = self.logic.session_maker() update_my_model(session, my_model_id) session.commit() The above code does issues the SQL to update mutable_attribute, but not normal_attribute. Everything works if I move the mutable_attribute change to after the normal_attribute change, remove it completely, or assign the return value of the update_my_model call to a variable. I have been able to determine that in that case that fails (and only in that case) MutableAttrInstanceState._cleanup is being called as update_my_model returns. However, during the session.commit(), the call to self.manager.new_instance(state=self) in MutableAttrInstanceState.__resurrect is not returning an object that has normal_attribute set. From what I can tell the MutableAttrInstanceState instance should know about the update to normal_attribute (InstanceState.modified_event is being called when it is set). But I'm not sure of the inner-workings of MutableAttrInstanceState,so I haven't been able to confirm this. What is not explained here is how the reference would be lost in the first place. The change to my_model.normal_attribute would place the object in the session's dirty list which results in a strong reference being created from the state to the object, which in turn is strongly referenced by the Session's identity map. My mutable_attribute is rather complex, so it is entirely possible that it is behaving badly in some why. However, this seems like a strange failure mode for an issue with that attribute? Anyone have ideas on what is going on here, or what my next steps should be to track it down? If any information I have not provided would be helpful, please let me know. oh well definitely, try to create a simple test case. For example, if I were to just take code like the above with a generic model, does that reproduce the issue ?If you think something about your mutable attribute is at fault, try replacing it with a plain dictionary and change a value within. I can't really imagine how anything regarding your mutable type could be involved unless it reaches out and modifies the environment containing my_model somehow. If just the code above, I'd do things like: def update_my_model(session, my_model_id): my_model = session.query(MyModel, id=my_model_id).one() my_model.mutable_attribute.mutate() assert my_model in session my_model.normal_attribute = 42 assert my_model in session.dirty return my_model -- 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] Unique Identifier for newly added child records?
Does SA maintain a usable unique identifier for newly added child records before the data is committed? I have a mapping of a one-many relationship using a foreign key. The detail (many side) records are in an instrumented list. I need to relate the items in this list to rows in an user interface object (a ttk.Treeview object). The detail table has a primary key that is maintained by SA, but until the data graph has been committed to the database, the corresponding fields detail objects are None. I can't just use the position of the detail item in the instrumented list because it is possible that the user can add and delete rows. The Treeview object does not re-use row identifiers for rows that have been deleted, so after the user has added and deleted detail rows, the Treeview rows will not match the instrumented list rows. Mark -- 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: Recursive problem
I solved the problem delaying the imports like: from zeppelinlib.screen.ScreenTest import Screen CRMContactInformation.id = Column(id, Integer, ForeignKey(Screen.id), primary_key=True) On Oct 25, 11:18 am, Alvaro Reinoso alvrein...@gmail.com wrote: Hey guys, I have a problem when I import classes from one to another. I have those classes in different modules: crm.py from CRMContactInformation import CRMContactInformation class CRM(rdb.Model): Set up crm table in the database rdb.metadata(metadata) rdb.tablename(crms) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) screen_id = Column(screen_id, Integer, ) contactInformation = relationship(CRMContactInformation, userlist=False, backref=crms) CRMContactInformation.py from CRM import CRM class CRMContactInformation(rdb.Model): Set up crm contact information table in the database rdb.metadata(metadata) rdb.tablename(crm_contact_informations) id = Column(id, Integer, ForeignKey(CRM.id), primary_key=True) owner = Column(owner, String(50)) . As you can see, I have a recursive problem because I import CRMContactInformation in CRM and CRM in CRMContactInformation. I got this error or similar: “AttributeError: ‘module’ object has no attribute ” I tried to change the imports importing the whole path. It didn't work out either. Is there any way I can use the metadata object to access the attributes of the tables? or another way to solve this? 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.
[sqlalchemy] Problems creating tables one to one relation
Hello, I get errors when I try to create tables one to one relation. Screen contains crm and crm contains more classes. The relation is one to one between crm, so I want to use the screen id as primary key in crm. And the relation is one to one between crm and some classes, I just added one class as example, so children of crm must contain a screen id as a primary key. When I try to make the last relation, it's when it breaks. I tried to use both, crm id and screen id. I didn't work. I get errors such as, UnmappedClassError when I try to use crm id in ContactInformation, and Could not determine join condition between parent/child tables on relationship CRM.contactInformation. Specify a 'primaryjoin' expression. If this is a many-to-many relationship, 'secondaryjoin' is needed as well when I try to use screen id in ContactInformation. These are my classes: class Screen(rdb.Model): Set up screens table in the database rdb.metadata(metadata) rdb.tablename(screens) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) crm = relationship(CRM, uselist=False, backref=screens) class CRM(rdb.Model): Set up crm table in the database rdb.metadata(metadata) rdb.tablename(crms) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) contactInformation = relationship(crm_contact_informations, uselist=False, backref=crms) class CRMContactInformation(rdb.Model): Set up crm contact information table in the database rdb.metadata(metadata) rdb.tablename(crm_contact_informations) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) owner = Column(owner, String(50)) ... -- 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] Unique Identifier for newly added child records?
On Oct 25, 2010, at 2:19 PM, Mark Erbaugh wrote: Does SA maintain a usable unique identifier for newly added child records before the data is committed? I have a mapping of a one-many relationship using a foreign key. The detail (many side) records are in an instrumented list. I need to relate the items in this list to rows in an user interface object (a ttk.Treeview object). The detail table has a primary key that is maintained by SA, but until the data graph has been committed to the database, the corresponding fields detail objects are None. I can't just use the position of the detail item in the instrumented list because it is possible that the user can add and delete rows. The Treeview object does not re-use row identifiers for rows that have been deleted, so after the user has added and deleted detail rows, the Treeview rows will not match the instrumented list rows. the ORM uses Python object identity to maintain associations before foreign key / primary key identifiers are assigned.So you could either associate your user interface objects directly with the related objects, or with their identity via a dictionary, otherwise if you need something that is durable beyond the scope of a single Session you'd need to assign a unique identifier (uuid.uuid4() is a good choice for this) or issue flush() so that primary key ids are available. -- 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] Problems creating tables one to one relation
all modules which include mapped classes must be imported before you attempt to initialize the mappings. If class A references B, but class B doesn't exist, you get errors like that, so importing the modules that contain both class A and B solves the issue. On Oct 25, 2010, at 2:55 PM, Alvaro Reinoso wrote: Hello, I get errors when I try to create tables one to one relation. Screen contains crm and crm contains more classes. The relation is one to one between crm, so I want to use the screen id as primary key in crm. And the relation is one to one between crm and some classes, I just added one class as example, so children of crm must contain a screen id as a primary key. When I try to make the last relation, it's when it breaks. I tried to use both, crm id and screen id. I didn't work. I get errors such as, UnmappedClassError when I try to use crm id in ContactInformation, and Could not determine join condition between parent/child tables on relationship CRM.contactInformation. Specify a 'primaryjoin' expression. If this is a many-to-many relationship, 'secondaryjoin' is needed as well when I try to use screen id in ContactInformation. These are my classes: class Screen(rdb.Model): Set up screens table in the database rdb.metadata(metadata) rdb.tablename(screens) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) crm = relationship(CRM, uselist=False, backref=screens) class CRM(rdb.Model): Set up crm table in the database rdb.metadata(metadata) rdb.tablename(crms) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) contactInformation = relationship(crm_contact_informations, uselist=False, backref=crms) class CRMContactInformation(rdb.Model): Set up crm contact information table in the database rdb.metadata(metadata) rdb.tablename(crm_contact_informations) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) owner = Column(owner, String(50)) ... -- 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] Unique Identifier for newly added child records?
On Oct 25, 2010, at 3:57 PM, Michael Bayer wrote: On Oct 25, 2010, at 2:19 PM, Mark Erbaugh wrote: Does SA maintain a usable unique identifier for newly added child records before the data is committed? I have a mapping of a one-many relationship using a foreign key. The detail (many side) records are in an instrumented list. I need to relate the items in this list to rows in an user interface object (a ttk.Treeview object). The detail table has a primary key that is maintained by SA, but until the data graph has been committed to the database, the corresponding fields detail objects are None. I can't just use the position of the detail item in the instrumented list because it is possible that the user can add and delete rows. The Treeview object does not re-use row identifiers for rows that have been deleted, so after the user has added and deleted detail rows, the Treeview rows will not match the instrumented list rows. the ORM uses Python object identity to maintain associations before foreign key / primary key identifiers are assigned.So you could either associate your user interface objects directly with the related objects, or with their identity via a dictionary, otherwise if you need something that is durable beyond the scope of a single Session you'd need to assign a unique identifier (uuid.uuid4() is a good choice for this) or issue flush() so that primary key ids are available. Thanks. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Problems creating tables one to one relation
I think it should be another problem because I have a file where I initialize all the tables, so that file contains all the tables. The tables are created properly, but when I try to create a Screen object (the main class, Screen - CRM - CRMContactInformation), I get this error: UnmappedClassError: Class 'Table('crm_contact_informations', MetaData(None), Column('id', Integer(), ForeignKey('crms.id'), table=crm_contact_informations, primary_key=True, nullable=False), Column('owner', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('owner_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('owner_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_manager', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), schema=None)' is not mapped On Oct 25, 3:58 pm, Michael Bayer mike...@zzzcomputing.com wrote: all modules which include mapped classes must be imported before you attempt to initialize the mappings. If class A references B, but class B doesn't exist, you get errors like that, so importing the modules that contain both class A and B solves the issue. On Oct 25, 2010, at 2:55 PM, Alvaro Reinoso wrote: Hello, I get errors when I try to create tables one to one relation. Screen contains crm and crm contains more classes. The relation is one to one between crm, so I want to use the screen id as primary key in crm. And the relation is one to one between crm and some classes, I just added one class as example, so children of crm must contain a screen id as a primary key. When I try to make the last relation, it's when it breaks. I tried to use both, crm id and screen id. I didn't work. I get errors such as, UnmappedClassError when I try to use crm id in ContactInformation, and Could not determine join condition between parent/child tables on relationship CRM.contactInformation. Specify a 'primaryjoin' expression. If this is a many-to-many relationship, 'secondaryjoin' is needed as well when I try to use screen id in ContactInformation. These are my classes: class Screen(rdb.Model): Set up screens table in the database rdb.metadata(metadata) rdb.tablename(screens) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) crm = relationship(CRM, uselist=False, backref=screens) class CRM(rdb.Model): Set up crm table in the database rdb.metadata(metadata) rdb.tablename(crms) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) contactInformation = relationship(crm_contact_informations, uselist=False, backref=crms) class CRMContactInformation(rdb.Model): Set up crm contact information table in the database rdb.metadata(metadata) rdb.tablename(crm_contact_informations) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) owner = Column(owner, String(50)) ... -- 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] Re: Problems creating tables one to one relation
On Oct 25, 2010, at 4:59 PM, Alvaro Reinoso wrote: I think it should be another problem because I have a file where I initialize all the tables, so that file contains all the tables. The tables are created properly, but when I try to create a Screen object (the main class, Screen - CRM - CRMContactInformation), I get this error: looks like you are passing a Table object, or the name of one, where a mapped class is expected. this would be the cause of that: contactInformation = relationship(crm_contact_informations, uselist=False, backref=crms) CRMContactInformation UnmappedClassError: Class 'Table('crm_contact_informations', MetaData(None), Column('id', Integer(), ForeignKey('crms.id'), table=crm_contact_informations, primary_key=True, nullable=False), Column('owner', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('owner_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('owner_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_manager', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), schema=None)' is not mapped On Oct 25, 3:58 pm, Michael Bayer mike...@zzzcomputing.com wrote: all modules which include mapped classes must be imported before you attempt to initialize the mappings. If class A references B, but class B doesn't exist, you get errors like that, so importing the modules that contain both class A and B solves the issue. On Oct 25, 2010, at 2:55 PM, Alvaro Reinoso wrote: Hello, I get errors when I try to create tables one to one relation. Screen contains crm and crm contains more classes. The relation is one to one between crm, so I want to use the screen id as primary key in crm. And the relation is one to one between crm and some classes, I just added one class as example, so children of crm must contain a screen id as a primary key. When I try to make the last relation, it's when it breaks. I tried to use both, crm id and screen id. I didn't work. I get errors such as, UnmappedClassError when I try to use crm id in ContactInformation, and Could not determine join condition between parent/child tables on relationship CRM.contactInformation. Specify a 'primaryjoin' expression. If this is a many-to-many relationship, 'secondaryjoin' is needed as well when I try to use screen id in ContactInformation. These are my classes: class Screen(rdb.Model): Set up screens table in the database rdb.metadata(metadata) rdb.tablename(screens) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) crm = relationship(CRM, uselist=False, backref=screens) class CRM(rdb.Model): Set up crm table in the database rdb.metadata(metadata) rdb.tablename(crms) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) contactInformation = relationship(crm_contact_informations, uselist=False, backref=crms) class CRMContactInformation(rdb.Model): Set up crm contact information table in the database rdb.metadata(metadata) rdb.tablename(crm_contact_informations) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) owner = Column(owner, String(50)) ... -- 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
[sqlalchemy] Problem updating mapped child class
Ok, the title doesn't say much, so I'll try to explain my problem with an example: I have two classes: class A(object) : pass class B(object) : pass which are bounded by a relation: a_tab = Table(...) b_tab = Table(...) b_tab.append_constraint(ForeignKeyConstraint([a_tab.c.id], [b_tab.c.a_id])) mapper(A, a_tab, properties={rel: relationship(B)}) mapper(B, b_tab) a_tab is already populated and has a lot of records (say 200 000), while b_tab is empty. So I load data from a_tab and populate b_tab this way: a_data = self.session.query(A).all() engine.execute(b_tab.insert(), [{a_id:x.id} for x in a_data]) now, of course, a_data[0].rel is [] Is there a way (possibly a fast way) to update the data of the relation 'rel' in the records of a_data? Thanks in advance for your help! -- 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: Problems creating tables one to one relation
It's fixed. I was my mistake, I didn't realize it. Thank you! On Oct 25, 5:19 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Oct 25, 2010, at 4:59 PM, Alvaro Reinoso wrote: I think it should be another problem because I have a file where I initialize all the tables, so that file contains all the tables. The tables are created properly, but when I try to create a Screen object (the main class, Screen - CRM - CRMContactInformation), I get this error: looks like you are passing a Table object, or the name of one, where a mapped class is expected. this would be the cause of that: contactInformation = relationship(crm_contact_informations, uselist=False, backref=crms) CRMContactInformation UnmappedClassError: Class 'Table('crm_contact_informations', MetaData(None), Column('id', Integer(), ForeignKey('crms.id'), table=crm_contact_informations, primary_key=True, nullable=False), Column('owner', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('owner_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('owner_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_manager', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('store_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager_phone', String(length=20, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), Column('it_manager_email', String(length=50, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), table=crm_contact_informations), schema=None)' is not mapped On Oct 25, 3:58 pm, Michael Bayer mike...@zzzcomputing.com wrote: all modules which include mapped classes must be imported before you attempt to initialize the mappings. If class A references B, but class B doesn't exist, you get errors like that, so importing the modules that contain both class A and B solves the issue. On Oct 25, 2010, at 2:55 PM, Alvaro Reinoso wrote: Hello, I get errors when I try to create tables one to one relation. Screen contains crm and crm contains more classes. The relation is one to one between crm, so I want to use the screen id as primary key in crm. And the relation is one to one between crm and some classes, I just added one class as example, so children of crm must contain a screen id as a primary key. When I try to make the last relation, it's when it breaks. I tried to use both, crm id and screen id. I didn't work. I get errors such as, UnmappedClassError when I try to use crm id in ContactInformation, and Could not determine join condition between parent/child tables on relationship CRM.contactInformation. Specify a 'primaryjoin' expression. If this is a many-to-many relationship, 'secondaryjoin' is needed as well when I try to use screen id in ContactInformation. These are my classes: class Screen(rdb.Model): Set up screens table in the database rdb.metadata(metadata) rdb.tablename(screens) id = Column(id, Integer, primary_key=True) title = Column(title, String(100)) crm = relationship(CRM, uselist=False, backref=screens) class CRM(rdb.Model): Set up crm table in the database rdb.metadata(metadata) rdb.tablename(crms) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) contactInformation = relationship(crm_contact_informations, uselist=False, backref=crms) class CRMContactInformation(rdb.Model): Set up crm contact information table in the database rdb.metadata(metadata) rdb.tablename(crm_contact_informations) id = Column(id, Integer, ForeignKey(screens.id), primary_key=True) owner = Column(owner, String(50)) ... -- 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
[sqlalchemy] InvalidRequestError: Unknown PG numeric type: 23
I've added a few extra columns to one of my tables (nothing fancy, just plain Integers and Floats) and created them manually in the db. Now every time I try to query an object from this table I get the error in the subject. Using declarative in SA 0.6.3 on PG8.4. Traceback below, any help is much appreciated! File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\query.py, line 1405, in __getitem__ return list(res) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\query.py, line 1565, in __iter__ return self._execute_and_instances(context) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\query.py, line 1570, in _execute_and_instances mapper=self._mapper_zero_or_none()) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\session.py, line 735, in execute clause, params or {}) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 1157, in execute params) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 1237, in _execute_clauseelement return self.__execute_context(context) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 1278, in __execute_context r = context.get_result_proxy()._autoclose() File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\dialects\postgresql\psycopg2.py, line 156, in get_result_proxy return base.ResultProxy(self) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 2169, in __init__ self._init_metadata() File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 2176, in _init_metadata self._metadata = ResultMetaData(self, metadata) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 2047, in __init__ result_processor(dialect, coltype) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\dialects\postgresql\psycopg2.py, line 99, in result_processor raise exc.InvalidRequestError(Unknown PG numeric type: %d % coltype) InvalidRequestError: Unknown PG numeric type: 23 -- 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] InvalidRequestError: Unknown PG numeric type: 23
this sounds like you're receiving an INTEGER column as a Numeric or Float. We should get 23 and such in there, thought we had gotten to this already but apparently not so ticket #1955 is added but for now make sure that column is declared as Integer. On Oct 25, 2010, at 7:35 PM, ellonweb wrote: I've added a few extra columns to one of my tables (nothing fancy, just plain Integers and Floats) and created them manually in the db. Now every time I try to query an object from this table I get the error in the subject. Using declarative in SA 0.6.3 on PG8.4. Traceback below, any help is much appreciated! File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\query.py, line 1405, in __getitem__ return list(res) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\query.py, line 1565, in __iter__ return self._execute_and_instances(context) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\query.py, line 1570, in _execute_and_instances mapper=self._mapper_zero_or_none()) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\orm\session.py, line 735, in execute clause, params or {}) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 1157, in execute params) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 1237, in _execute_clauseelement return self.__execute_context(context) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 1278, in __execute_context r = context.get_result_proxy()._autoclose() File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\dialects\postgresql\psycopg2.py, line 156, in get_result_proxy return base.ResultProxy(self) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 2169, in __init__ self._init_metadata() File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 2176, in _init_metadata self._metadata = ResultMetaData(self, metadata) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\engine\base.py, line 2047, in __init__ result_processor(dialect, coltype) File C:\Program Files\Python\lib\site-packages\sqlalchemy-0.6.3- py2.6.egg\sqlalchemy\dialects\postgresql\psycopg2.py, line 99, in result_processor raise exc.InvalidRequestError(Unknown PG numeric type: %d % coltype) InvalidRequestError: Unknown PG numeric type: 23 -- 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.