[sqlalchemy] query doctest
Hi, in a doctest I have:: str(q) == str(query.filter(User.first_name == 'ed')) True that works but the following fails, (both where built starting from the same session.query(User) q == query.filter(User.first_name == 'ed') True in another place comparing str fails just because a join has ON join condition inverted (but semantically equivalent). What's the correct way to test if two queries are semantically equivalent? Thanks sandro *:-) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] metadata reflecting all schemas
Hi I was wondering if there was a way to reflect all schemas in the metadata, or get a list of schemas in the database with out querying the catalog for postgresql. Regards, Jar --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: metadata reflecting all schemas
Or maybe this is my problem /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'USERNAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'SCHEMA_NAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'TABLE_NAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'FIELD_NAME' self.dialect.reflecttable(conn, table, include_columns) I use the 'name' column data type for my columns. Its a postgresql database. engine = create_engine('postgres://.') engine metadata = MetaData() metadata.reflect(engine,'') for t in metadata.tables.values(): print t.fullname for c in t.columns: print \t + c.name I get a partial listing which as two different schemas in it. Is the reflect bombing out when it hits the above errors? If so, I will need to try and get sqlalchemy to accept this field type. Regards, Jar On Nov 29, 10:34 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi I was wondering if there was a way to reflect all schemas in the metadata, or get a list of schemas in the database with out querying the catalog for postgresql. Regards, Jar --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: query doctest
On Nov 29, 2008, at 6:22 AM, sandro dentella wrote: Hi, in a doctest I have:: str(q) == str(query.filter(User.first_name == 'ed')) True that works but the following fails, (both where built starting from the same session.query(User) q == query.filter(User.first_name == 'ed') True in another place comparing str fails just because a join has ON join condition inverted (but semantically equivalent). What's the correct way to test if two queries are semantically equivalent? I usually produce their results via all() or similar and compare those. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: New instance ExtraStat with identity key (...) conflicts with persistent instance ExtraStat
Sorry, I didn't realize I had done that, I thought I had started a new post. My apologies. -Original Message- From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Friday, November 28, 2008 11:45 PM To: sqlalchemy Subject: [sqlalchemy] Re: New instance ExtraStat with identity key (...) conflicts with persistent instance ExtraStat What was your justification of changing the name of my thread to a completely different topic instead of starting a new thread? I don't think thats good etiquette. On Nov 29, 5:22 am, Doug Farrell [EMAIL PROTECTED] wrote: Hi all, I'm having a problem with a new instance of a relation conflicting with an existing instance. I'm using SA 0.5rc with Sqlite3. Here are my simplified classes: class Stat(sqladb.Base): __tablename__ = stats name = Column(String(32), primary_key=True) total= Column(Integer) created = Column(DateTime, default=datetime.datetime.now()) updated = Column(DateTime) states = Column(PickleType, default={}) extraStats = relation(ExtraStat, backref=stat) class ExtraStat(sqladb.Base): __tablename__ = extrastats name = Column(String(32), ForeignKey(stats.name), primary_key=True) total= Column(Integer) created = Column(DateTime, default=datetime.datetime.now()) updated = Column(DateTime) states = Column(PickleType, default={}) The above Stat class has a one-to-many relationship with the ExtraStat class (which I think I've implemented correctly). Later in the program I create an in memory data model that has as part of it's components two dictionaries that contain Stat instances. Those Stat instances have relationships to ExtraStat instances. My problem comes in the following when I'm trying to update the data in those instances/tables. Here is a section of code that throws the exception: pressName = press%s % pressNum # add new ExtraStat instances as relations self._addProductStatsPress(productType, pressName) self._addPressStatsProduct(pressName, productType) try: extraStat = session.query(Stat). \ filter(Stat.name==productType). \ join(extraStats). \ filter(ExtraStat.name==pressName).one() except: extraStat = ExtraStat(pressName, ExtraStat.PRESS_TYPE) self.productStats[productType].extraStats.append(extraStat) extraStat.states.setdefault(sstate, 0) extraStat.states[sstate] += 1 extraStat.updated = now extraStat = session.merge(extraStat) try: extraStat = session.query(Stat). \ filter(Stat.name==pressName). \ join(extraStats). \ filter(ExtraStat.name==productType).one() throws exception right here except: extraStat = ExtraStat(productType, ExtraStat.PRODUCT_TYPE) self.pressStats[pressName].extraStats.append(extraStat) extraStat.states.setdefault(sstate, 0) extraStat.states[sstate] += 1 extraStat.updated = now The marked area is wear it throws the exception. I'm not sure what to do here to get past this, any help or ideas would be greatly appreciated. The exact exception is as follows: Sqlalchemy.orm.exc.FlushError: New instance [EMAIL PROTECTED] With identity key (class '__main__.ExtraStat',(u'C',)) conflicts with persistent instance [EMAIL PROTECTED] Thanks! Doug --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] New instance ExtraStat with identity key (...) conflicts with persistent instance ExtraStat
Hi all, I'm having a problem with a new instance of a relation conflicting with an existing instance. I'm using SA 0.5rc with Sqlite3. Here are my simplified classes: class Stat(sqladb.Base): __tablename__ = stats name = Column(String(32), primary_key=True) total= Column(Integer) created = Column(DateTime, default=datetime.datetime.now()) updated = Column(DateTime) states = Column(PickleType, default={}) extraStats = relation(ExtraStat, backref=stat) class ExtraStat(sqladb.Base): __tablename__ = extrastats name = Column(String(32), ForeignKey(stats.name), primary_key=True) total= Column(Integer) created = Column(DateTime, default=datetime.datetime.now()) updated = Column(DateTime) states = Column(PickleType, default={}) The above Stat class has a one-to-many relationship with the ExtraStat class (which I think I've implemented correctly). Later in the program I create an in memory data model that has as part of it's components two dictionaries that contain Stat instances. Those Stat instances have relationships to ExtraStat instances. My problem comes in the following when I'm trying to update the data in those instances/tables. Here is a section of code that throws the exception: pressName = press%s % pressNum # add new ExtraStat instances as relations self._addProductStatsPress(productType, pressName) self._addPressStatsProduct(pressName, productType) try: extraStat = session.query(Stat). \ filter(Stat.name==productType). \ join(extraStats). \ filter(ExtraStat.name==pressName).one() except: extraStat = ExtraStat(pressName, ExtraStat.PRESS_TYPE) self.productStats[productType].extraStats.append(extraStat) extraStat.states.setdefault(sstate, 0) extraStat.states[sstate] += 1 extraStat.updated = now extraStat = session.merge(extraStat) try: extraStat = session.query(Stat). \ filter(Stat.name==pressName). \ join(extraStats). \ filter(ExtraStat.name==productType).one() throws exception right here except: extraStat = ExtraStat(productType, ExtraStat.PRODUCT_TYPE) self.pressStats[pressName].extraStats.append(extraStat) extraStat.states.setdefault(sstate, 0) extraStat.states[sstate] += 1 extraStat.updated = now The marked area is wear it throws the exception. I'm not sure what to do here to get past this, any help or ideas would be greatly appreciated. The exact exception is as follows: Sqlalchemy.orm.exc.FlushError: New instance [EMAIL PROTECTED] With identity key (class '__main__.ExtraStat',(u'C',)) conflicts with persistent instance [EMAIL PROTECTED] Thanks! Doug --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: metadata reflecting all schemas
the type is not recognized but the reflection operation should succeed. thats why you're only getting a warning on those. On Nov 29, 2008, at 8:00 AM, [EMAIL PROTECTED] wrote: Or maybe this is my problem /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'USERNAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'SCHEMA_NAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'TABLE_NAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'FIELD_NAME' self.dialect.reflecttable(conn, table, include_columns) I use the 'name' column data type for my columns. Its a postgresql database. engine = create_engine('postgres://.') engine metadata = MetaData() metadata.reflect(engine,'') for t in metadata.tables.values(): print t.fullname for c in t.columns: print \t + c.name I get a partial listing which as two different schemas in it. Is the reflect bombing out when it hits the above errors? If so, I will need to try and get sqlalchemy to accept this field type. Regards, Jar On Nov 29, 10:34 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi I was wondering if there was a way to reflect all schemas in the metadata, or get a list of schemas in the database with out querying the catalog for postgresql. Regards, Jar --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: metadata reflecting all schemas
Thanks Michael If its just a warning and its supposed to continue past it, Why doesn't it finish reflecting all the tables in all the schemas instead of a few tables in two schemas. I think it retrieved all the tables in the first schema which i specified and followed the foreign keys to retrieve the metadata for the second tables. Any suggestions on how i can reflect a list of schemas or make it reflect all the schemas? it didn't like '%' as the schema name. On Nov 30, 4:42 am, Michael Bayer [EMAIL PROTECTED] wrote: the type is not recognized but the reflection operation should succeed. thats why you're only getting a warning on those. On Nov 29, 2008, at 8:00 AM, [EMAIL PROTECTED] wrote: Or maybe this is my problem /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'USERNAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'SCHEMA_NAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'TABLE_NAME' self.dialect.reflecttable(conn, table, include_columns) /home/jchesney/workspace/sqlalchemy/lib/sqlalchemy/engine/base.py: 1237: SAWarning: Did not recognize type 'name' of column 'FIELD_NAME' self.dialect.reflecttable(conn, table, include_columns) I use the 'name' column data type for my columns. Its a postgresql database. engine = create_engine('postgres://.') engine metadata = MetaData() metadata.reflect(engine,'') for t in metadata.tables.values(): print t.fullname for c in t.columns: print \t + c.name I get a partial listing which as two different schemas in it. Is the reflect bombing out when it hits the above errors? If so, I will need to try and get sqlalchemy to accept this field type. Regards, Jar On Nov 29, 10:34 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi I was wondering if there was a way to reflect all schemas in the metadata, or get a list of schemas in the database with out querying the catalog for postgresql. Regards, Jar --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: metadata reflecting all schemas
On Nov 29, 2008, at 6:05 PM, [EMAIL PROTECTED] wrote: Thanks Michael If its just a warning and its supposed to continue past it, Why doesn't it finish reflecting all the tables in all the schemas instead of a few tables in two schemas. that would be a different issue. But I would note that metadata.reflect() only reflects one schema at a time, either the tables within the default schema, or those within the schema name which you specify. I think it retrieved all the tables in the first schema which i specified and followed the foreign keys to retrieve the metadata for the second tables. that's what it would do, yup. Any suggestions on how i can reflect a list of schemas or make it reflect all the schemas? it didn't like '%' as the schema name. you have to retreive the list of desired schemas manually, then call reflect() for each one. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: New instance ExtraStat with identity key (...) conflicts with persistent instance ExtraStat
Simon's suggestion about the duplicate name still holds. Your relation from Stat-ExtraStat currently needs to be one-to-one since you cannot have more than one ExtraStat referencing a single Stat, due to the PK constraint on ExtraStat.name. The error is raising at the point of query() since autoflush is kicking in - use session.flush() to isolate the error. On Nov 29, 2008, at 12:18 PM, Doug Farrell wrote: Hi all, I'm having a problem with a new instance of a relation conflicting with an existing instance. I'm using SA 0.5rc with Sqlite3. Here are my simplified classes: class Stat(sqladb.Base): __tablename__ = stats name = Column(String(32), primary_key=True) total= Column(Integer) created = Column(DateTime, default=datetime.datetime.now()) updated = Column(DateTime) states = Column(PickleType, default={}) extraStats = relation(ExtraStat, backref=stat) class ExtraStat(sqladb.Base): __tablename__ = extrastats name = Column(String(32), ForeignKey(stats.name), primary_key=True) total= Column(Integer) created = Column(DateTime, default=datetime.datetime.now()) updated = Column(DateTime) states = Column(PickleType, default={}) The above Stat class has a one-to-many relationship with the ExtraStat class (which I think I've implemented correctly). Later in the program I create an in memory data model that has as part of it's components two dictionaries that contain Stat instances. Those Stat instances have relationships to ExtraStat instances. My problem comes in the following when I'm trying to update the data in those instances/ tables. Here is a section of code that throws the exception: pressName = press%s % pressNum # add new ExtraStat instances as relations self._addProductStatsPress(productType, pressName) self._addPressStatsProduct(pressName, productType) try: extraStat = session.query(Stat). \ filter(Stat.name==productType). \ join(extraStats). \ filter(ExtraStat.name==pressName).one() except: extraStat = ExtraStat(pressName, ExtraStat.PRESS_TYPE) self.productStats[productType].extraStats.append(extraStat) extraStat.states.setdefault(sstate, 0) extraStat.states[sstate] += 1 extraStat.updated = now extraStat = session.merge(extraStat) try: extraStat = session.query(Stat). \ filter(Stat.name==pressName). \ join(extraStats). \ filter(ExtraStat.name==productType).one() throws exception right here except: extraStat = ExtraStat(productType, ExtraStat.PRODUCT_TYPE) self.pressStats[pressName].extraStats.append(extraStat) extraStat.states.setdefault(sstate, 0) extraStat.states[sstate] += 1 extraStat.updated = now The marked area is wear it throws the exception. I'm not sure what to do here to get past this, any help or ideas would be greatly appreciated. The exact exception is as follows: Sqlalchemy.orm.exc.FlushError: New instance [EMAIL PROTECTED] With identity key (class '__main__.ExtraStat',(u'C',)) conflicts with persistent instance [EMAIL PROTECTED] Thanks! Doug --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---