Re: [sqlalchemy] Need for ImplicitForeignKeyConstraint
So IFKC(ImplicitForeignKeyConstraint) should not have inherited FKC? I did it so that it could seamlessly be passed into Table() and __table_args__. PS: Everyone, the repo is at https://bitbucket.org/fayaz/implicit On Friday, August 05, 2011 07:31:44 PM Michael Bayer wrote: yeah wow I just saw that.Can you use table.add_constraint(fk) instead of _set_parent() ? On Aug 5, 2011, at 1:34 AM, Fayaz Yusuf Khan wrote: So I had been working on this tiny project now and then. And here's the poc. http://paste.pound-python.org/show/10578/ I think I'm somewhat misusing the _set_parent() here though. On Sunday, July 24, 2011 06:52:45 PM Michael Bayer wrote: On Jul 24, 2011, at 8:39 AM, Fayaz Yusuf Khan wrote: The problem with using different mixins is that you lose out on a lot of code reusability. In my case, I have a 'user' column that appears in almost all table declarations. To have a separate mixin class for each joint-table inheritance would destroy the purpose of having a mixin altogether. In your example you can simply use CMixin and TMixin separately instead of inheriting them from one another, then apply CMixin and TMixin directly to C individually.That makes more sense here since for every class X which you want to have user, you'd apply CMixin explicitly. The more I look at this the more it seems completely correct to me. Mixins and declarative do a lot , and sticking to Python's regular rules for inheritance is what makes them great. Perhaps, there should be a shorthand for implicitly creating columns along with foreign key constraints? So something like ImplicitForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp'], primary_key=True) should lead to the creation of Column('user', String, primary_key=True), Column('timestamp',Integer, autoincrement=False, primary_key=True), ForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp']) Not something for core but certainly something you could provide yourself (use append_column()). SQLA's APIs try to remain explicit about things leaving implicit helper layers as an external task (hence relationship + ForeignKey, as opposed to the all in one demo I did at http://techspot.zzzeek.org/2011/05/17/magic-a-new-orm/ , etc) -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Re: Cascade Deletes
Il 06/08/11 00.32, Aviv Giladi ha scritto: Hi Stefano, I create and add a Rating and Subrating (both end up in the DB no problem). Then, I call session.delete(rating_obj) and commit it. I look at the DB, and the Rating is gone, but the SubRating is still there. The DB shows that the Rating has the correct Subrating's ID.. Hi Aviv, I attached the code you sent me. I move 'cascade' as I told you and everything works. See the log that I pasted at the bottom of the script. -- Ing. Stefano Fontanelli Asidev S.r.l. Via Osteria Bianca, 108/A 50053 Empoli (Firenze) Tel. (+39) 333 36 53 294 Fax. (+39) 0571 1 979 978 E-mail: s.fontane...@asidev.com Web: www.asidev.com Skype: stefanofontanelli -- 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. from sqlalchemy import * from sqlalchemy.orm import backref from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() subrating_subratingproperty = Table('subrating_subratingproperty_association', Base.metadata, Column('subrating_id', Integer, ForeignKey('subratings.id')), Column('subrating_property_id', Integer, ForeignKey('subrating_properties.id') )) class SubRatingProperty(Base): __tablename__ = 'subrating_properties' id = Column(Integer, primary_key=True) name = Column(Unicode(32), unique=True) subratings = relationship(SubRating, secondary=subrating_subratingproperty, backref=subrating_properties) class Rating(Base): __tablename__ = 'ratings' id = Column(Integer, primary_key=True) name = Column(Unicode(32), unique=True) subrating_id = Column(Integer, ForeignKey('subratings.id')) subrating = relationship(SubRating, cascade=all, delete-orphan, backref=backref(rating, uselist=False)) class SubRating(Base): __tablename__ = 'subratings' id = Column(Integer, primary_key=True) name = Column(Unicode(32), unique=True) if __name__ == '__main__': from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session from sqlalchemy.orm import sessionmaker engine = create_engine('sqlite:///', echo=True) Base.metadata.create_all(engine) session = scoped_session(sessionmaker()) session.configure(bind=engine) subrating = SubRating(name=u'My First Subrating') rating = Rating(name=u'My First Rating', subrating=subrating) session.add(rating) session.flush() assert rating.subrating != None assert subrating.rating != None session.commit() assert rating.subrating == subrating assert subrating.rating == rating rating = session.query(Rating).first() session.delete(rating) session.flush() session.commit() assert session.query(Rating).all() == [] assert session.query(SubRating).all() == [] $ python test.py 2011-08-06 12:13:02,959 INFO sqlalchemy.engine.base.Engine PRAGMA table_info(subratings) 2011-08-06 12:13:02,959 INFO sqlalchemy.engine.base.Engine () 2011-08-06 12:13:02,960 INFO sqlalchemy.engine.base.Engine PRAGMA table_info(subrating_properties) 2011-08-06 12:13:02,960 INFO sqlalchemy.engine.base.Engine () 2011-08-06 12:13:02,960 INFO sqlalchemy.engine.base.Engine PRAGMA table_info(ratings) 2011-08-06 12:13:02,960 INFO sqlalchemy.engine.base.Engine () 2011-08-06 12:13:02,960 INFO sqlalchemy.engine.base.Engine PRAGMA table_info(subrating_subratingproperty_association) 2011-08-06 12:13:02,960 INFO sqlalchemy.engine.base.Engine () 2011-08-06 12:13:02,961 INFO sqlalchemy.engine.base.Engine CREATE TABLE subratings ( id INTEGER NOT NULL, name VARCHAR(32), PRIMARY KEY (id), UNIQUE (name) ) 2011-08-06 12:13:02,961 INFO sqlalchemy.engine.base.Engine () 2011-08-06 12:13:02,961 INFO sqlalchemy.engine.base.Engine COMMIT 2011-08-06 12:13:02,962 INFO sqlalchemy.engine.base.Engine CREATE TABLE subrating_properties ( id INTEGER NOT NULL, name VARCHAR(32), PRIMARY KEY (id), UNIQUE (name) ) 2011-08-06 12:13:02,962 INFO sqlalchemy.engine.base.Engine () 2011-08-06 12:13:02,962 INFO sqlalchemy.engine.base.Engine COMMIT 2011-08-06 12:13:02,962 INFO sqlalchemy.engine.base.Engine CREATE TABLE ratings ( id INTEGER NOT NULL, name VARCHAR(32), subrating_id INTEGER, PRIMARY KEY (id), UNIQUE (name), FOREIGN KEY(subrating_id)
Re: [sqlalchemy] Declarative Field Type 'Alias'
On 08/05/2011 10:46 PM, Mark Erbaugh wrote: This is more of a Python issue than a SA issue, but I had trouble getting this to work. I did, but the code seems a little awkard to mesigh. In addition to the requirements already, I also wanted toe default value to be a class level 'constant'. The problem, as I see it, is that since the class definition isn't complete, it's namespace isn't avaialble. Since the default value 'constant' is a class data member, it would make sense if the function were a @classmethod, but I couldn't get python to accept: class Table(Base): ... DEFAULT = 2 @classmethod def CustomColumn(cls): return Column(Integer, default=DEFAULT) that should be cls.DEFAULT ... field1 = CustomColumn() Python complained 'classmethod object is not callable' on the last line above. You can only call a class method on a class. In this case that would be Table.CustomColumn(). However since the Table class is not available at this point you can't do that. You can do this sort of thing with metaclasses, but I would not recommend going down that paht. What I finally ended up with that works is: class Table(Base): ... DEFAULT = 2 def CustomColumn(default=DEFAULT): return Column(Integer, default=default) ... field1 = CustomColumn() That looks like a pretty good solution. Wichert. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Default values
You can get to the column default value. class MyTable(Base): __tablename__ = 'table' id = Column(Integer, primary_key=True) name = Column(String, default='new name') def __init__(self, name=None): if name is not None: self.name = name else: self.name = getDefault(self, 'name') def getDefault(instance, colName): col = instance.__table__.c[colName] if col.default is not None: dflt = col.default.arg else: dflt = None return dflt The column attribute you need to check for could also be server_default if that is what you specified in the metadata. Be careful, if the defaults are specified as server side defaults in an existing database where the DDL is not generated from SQLAlchemy, the SQLAlchemy metadata doesn't necessarily know about them unless you reflected the table or you coded the server side default in your code by hand. -- Mike Conley -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Default values
On Aug 6, 2011, at 7:18 AM, Mike Conley wrote: You can get to the column default value. class MyTable(Base): __tablename__ = 'table' id = Column(Integer, primary_key=True) name = Column(String, default='new name') def __init__(self, name=None): if name is not None: self.name = name else: self.name = getDefault(self, 'name') def getDefault(instance, colName): col = instance.__table__.c[colName] if col.default is not None: dflt = col.default.arg else: dflt = None return dflt Mike, Thanks. I adapted your code: def __init__(self): for col in self.__table__.c: if col.default is not None: self.__setattr__(col.key, col.default.arg) Mark -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Does orm.synonym's comparator_factory work?
As a follow up to this thread, I've seen in the code in version 0.7.2 that comparator_factory is no longer working for CompositeProperty. Is this a bug? Reference: http://www.sqlalchemy.org/trac/browser/lib/sqlalchemy/orm/descriptor_props.py#L75 You can see that in the constructor there is no way in specifying a comporator_factory. If this is not a bug, then I guess the documentation is wrong: http://www.sqlalchemy.org/docs/orm/mapper_config.html#sqlalchemy.orm.composite Thanks -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/j5XfGj-vyZAJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Does orm.synonym's comparator_factory work?
I cannot locate the rationale for this despite a dim memory that there was one, so this is a bug with ticket 2248 created at http://www.sqlalchemy.org/trac/ticket/2248 , with a restorative patch attached. If someone can provide tests for the attached patch using a custom comparator and there's no issue (I'm assuming you might have one) the feature can be re-implemented. On Aug 6, 2011, at 6:10 PM, Arturo Sevilla wrote: As a follow up to this thread, I've seen in the code in version 0.7.2 that comparator_factory is no longer working for CompositeProperty. Is this a bug? Reference: http://www.sqlalchemy.org/trac/browser/lib/sqlalchemy/orm/descriptor_props.py#L75 You can see that in the constructor there is no way in specifying a comporator_factory. If this is not a bug, then I guess the documentation is wrong: http://www.sqlalchemy.org/docs/orm/mapper_config.html#sqlalchemy.orm.composite Thanks -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/j5XfGj-vyZAJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Cascade Deletes
Stefano, Thanks! Your script helped me narrow down the problem. My Rating object has multiple Subrating objects. So in my real code, I have something like: class SubRating1(Base): __tablename__ = 'subratings1' id = Column(Integer, primary_key=True) name = Column(Unicode(32), unique=True) class SubRating2(Base): __tablename__ = 'subratings2' id = Column(Integer, primary_key=True) name = Column(Unicode(32), unique=True) class SubRating3(Base): __tablename__ = 'subratings3' id = Column(Integer, primary_key=True) name = Column(Unicode(32), unique=True) And then my Rating looks like: class Rating(Base): __tablename__ = 'ratings' id = Column(Integer, primary_key=True) name = Column(Unicode(32), unique=True) subrating1_id = Column(Integer, ForeignKey('subratings1.id')) subrating1 = relationship(SubRating1, backref=backref(rating, cascade=all, delete-orphan, uselist=False)) subrating2_id = Column(Integer, ForeignKey('subratings2.id')) subrating2 = relationship(SubRating2, backref=backref(rating, cascade=all, delete-orphan, uselist=False)) subrating3_id = Column(Integer, ForeignKey('subratings3.id')) subrating3 = relationship(SubRating3, backref=backref(rating, cascade=all, delete-orphan, uselist=False)) Everything works great when I create and assign all 3 subratings to the rating object before I add it to the session. However, I need to be able to create a Rating that only has 1 or 2 subratings, and the other subratings absent. When I do that, SQLAlchemy tells me: InterfaceError: (InterfaceError) Error binding parameter 0 - probably unsupported type. u'SELECT SubRating2.id AS subrating2_id \nFROM subratings2 \nWHERE subrating2.id = ?' (symbol 'NEVER_SET,) The above error is when I set Ratings's subrating1 and subrating3, but not subrating2. How do I avoid this error? On Aug 6, 6:16 am, Stefano Fontanelli s.fontane...@asidev.com wrote: Il 06/08/11 00.32, Aviv Giladi ha scritto: Hi Stefano, I create and add a Rating and Subrating (both end up in the DB no problem). Then, I call session.delete(rating_obj) and commit it. I look at the DB, and the Rating is gone, but the SubRating is still there. The DB shows that the Rating has the correct Subrating's ID.. Hi Aviv, I attached the code you sent me. I move 'cascade' as I told you and everything works. See the log that I pasted at the bottom of the script. -- Ing. Stefano Fontanelli Asidev S.r.l. Via Osteria Bianca, 108/A 50053 Empoli (Firenze) Tel. (+39) 333 36 53 294 Fax. (+39) 0571 1 979 978 E-mail: s.fontane...@asidev.com Web:www.asidev.com Skype: stefanofontanelli test.py 7KViewDownload -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Does orm.synonym's comparator_factory work?
I will see if later today I can provide some tests, or by tomorrow. I think the basic rationale is that a composite property should behave like a regular mapped property, and those have a comparator_factory, although I know this is probable a lame argument. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/J9HVsxHYg60J. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] Does orm.synonym's comparator_factory work?
oh...sorry I meant the rationale for it being removed in 0.7, it had something to do with the internals of the whole thing. Perhaps I just forgot to re-implement, not sure. On Aug 6, 2011, at 7:24 PM, Arturo Sevilla wrote: I will see if later today I can provide some tests, or by tomorrow. I think the basic rationale is that a composite property should behave like a regular mapped property, and those have a comparator_factory, although I know this is probable a lame argument. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/J9HVsxHYg60J. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.