[sqlalchemy] Re: column_property with load_only and inheritance
Hi Michael, thank you for your quick response! Yeah, it seems that I got confused about the "load_only" method - it doesn't load at all. I've tried to put together an easier example to show the problem. It seems that it is related to the joinedload. import sqlalchemy.orm import sqlalchemy.inspection entity = model.session.query( model.Asset ).options( sqlalchemy.orm.joinedload( 'parent' ) ).first() state = sqlalchemy.inspection.inspect(entity.parent) for attribute in state.attrs: is_loaded = ( attribute.loaded_value is not sqlalchemy.orm.base.NO_VALUE ) if is_loaded: print attribute.key And if I omit it, it load s fine. Here is a simplified version of my model: class Context(Base): '''Represent a context.''' context_type = Column(Unicode(32), nullable=False) __mapper_args__ = { 'polymorphic_on': context_type, 'polymorphic_identity': 'context' } @declared_attr def __tablename__(cls): return 'context' name = Column(Unicode(255), default=u'', nullable=False) @declared_attr def id(cls): return Column(CHAR(36), primary_key=True, default=lambda: str(uuid ())) @classmethod def __declare_last__(cls): '''Return link expression query.''' # Import this module. from . import context context = aliased(context.Context.__table__) # My real use-case is more complicated and involves a lot of joinst to other tables, but this example reproduces the # issue. cls.link = column_property( sqlalchemy.select( [context.c.name + ' ' + context.c.context_type], from_obj=[context] ).where( context.c.id == cls.id ).label('link') ) class Task(Context): '''Represent a task.''' @declared_attr def __tablename__(cls): return 'task' @declared_attr def __table_args__(cls): return { 'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8' } taskid = Column( types.CHAR(36), ForeignKey('context.id'), primary_key=True ) __mapper_args__ = { 'polymorphic_identity': 'task' } class Asset(Base): '''Represent an Asset.''' @declared_attr def __tablename__(cls): return 'asset' @declared_attr def __table_args__(cls): return { 'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8' } context_id = sqlalchemy.Column( sqlalchemy.CHAR(36), sqlalchemy.ForeignKey('context.id') ) parent = relationship('Context', backref=backref('assets')) I hope this example makes more sense. Can you see any obvious problems with my approach and why it wouldn't work? If I add the link as a declared_attr instead it does work: @declared_attr def link(cls): return column_property(cls.name + ' ' + cls.context_type) However, my real-life use-case link is more complicated and I need to do imports that would cause circular import errors if I used declared_attr. Best regards, Mattias L On Monday, November 2, 2015 at 3:16:07 PM UTC+1, Mattias Lagergren wrote: > > Hi, > > I'm trying to use load_only and joinedload on a relationship > model.Asset.parent. The parent relation is polymorphic and can be either > Task or Project with the common Base class called Context. > > import sqlalchemy.orm > import sqlalchemy.inspection > > entity = model.session.query( > model.Asset > ).options( > sqlalchemy.orm.joinedload('parent').load_only( > 'context_type', 'name', 'link' > ) > ).first() > > state = sqlalchemy.inspection.inspect(entity.parent) > for attribute in state.attrs: > is_loaded = ( > attribute.loaded_value is not > sqlalchemy.orm.base.NO_VALUE > ) > if is_loaded: > print attribute.key > > # Output: > id > taskid > name > context_type > > > The id, name and context_type is from Context. And taskid is primary key > on the taskid and is a foreignkey to the context.id. As you can see > "name" loads fine but "link" attribute is not loaded. The "link" column is > added as a column_property to Context using a __declare_last__. > > These are simplified versions of the classes: > > > class Context(Base): > '''Represent a context.''' > context_type = Column(Unicode(32), nullable=False) > > __mapper_args__ = { > 'polymorphic_on': context_type, > 'polymorphic_identity': 'context' > } > > name = Column(Unicode(255), default=u'', nullable=False) > > @declared_attr > def id(cls): > return
[sqlalchemy] Re: N-Triples to MySQL
You probably need to ask this on an rdflib forum. Looking at your code, you're just tossing a SqlAlchemy engine into rdflib. You probably need to integrate some sort of plugin, such as https://github.com/RDFLib/rdflib-sqlalchemy -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] N-Triples to MySQL
Hey, I'm trying to convert a N-Triples/.nt file to MySQL, with rdflib, sqlalchemy and mysql.connector with Python. This is my code: from rdflib import Graph import sqlalchemy #1.0.9 print(sqlalchemy.__version__) engine = sqlalchemy.create_engine( 'mysql+mysqlconnector://root:@localhost:3306/ksaat', pool_recycle=3600) connection = engine.connect() # print(connection) g = Graph(engine, identifier="ksaat") g.open(connection, create=True) g.parse("input", format="nt") g.store() #3110284 print(len(g)) I'm getting this error message: No plugin registered for (Engine(mysql+mysqlconnector://root:***@localhost:3306/ksaat), ) What can I do to make my code work? -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] compare a object/instance against its data in the database
On 11/03/2015 03:21 PM, c.bu...@posteo.jp wrote: Is there a way to find out if a persistent sqlalchemy mapped object was modified? It means I recieve a object (persistent, with identiy) from the database. Then the user (maybe!) modify its data in a dialog-window. obj = session.query(MyClass).first() LetTheUserDoSomethingWithIt(obj) if obj.is_modified() Is there a way to find out if the object in memory/RAM was modified compared to its instance in the database itself? I don't want to compare each attribute against the data in the dialog-window. session.is_modified(obj) will do this for you: http://docs.sqlalchemy.org/en/rel_1_0/orm/session_api.html?highlight=is_modified#sqlalchemy.orm.session.Session.is_modified read the docs carefully, this method can be tricky. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] SQLAlchemy: Database Access Using Python - Developer's Library book
I came across this during a search, and in the four different sites I've checked, I've seen four different release dates varying from 2008 to 2018, for example: http://www.amazon.com/SQLAlchemy-Database-Access-Developers-Library/dp/0132364670 (which has 2018 for the release date). So I'm curious... Is this a very old book (2008) or a book that has several years to be released (2018)? Does anyone actually know which of the various dates are most likely correct? :) (I've seen a few around May-June of 2016, which seems the most likely, but...) Given one of the authors of the book, I'm hoping I might be able to find out more here. :) -- Ken Lareau -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] compare a object/instance against its data in the database
Is there a way to find out if a persistent sqlalchemy mapped object was modified? It means I recieve a object (persistent, with identiy) from the database. Then the user (maybe!) modify its data in a dialog-window. obj = session.query(MyClass).first() LetTheUserDoSomethingWithIt(obj) if obj.is_modified() Is there a way to find out if the object in memory/RAM was modified compared to its instance in the database itself? I don't want to compare each attribute against the data in the dialog-window. -- -BEGIN PGP PUBLIC KEY BLOCK- Version: GnuPG v1 mQENBFQIluABCACfPwAhRAwFD3NXgv5CtVUGSiqdfJGVViVBqaKd+14E0pASA0MU G0Ewj7O7cGy/ZIoiZ0+lIEZmzJKHfuGwYhXjR/PhnUDrQIHLBvh9WuD6JQuULXfH kXtVm/i9wm76QAcvr2pwYgNzhcJntUHl2GcgnInYbZDeVmg+p9yIPJjuq73/lRS3 0/McgNoFOBhKK/S6STQuFyjr9OyJyYd1shoM3hmy+kg0HYm6OgQBJNg92WV9jwGe GzlipvEp2jpLwVsTxYir2oOPhfd9D1fC9F/l/3gXbfjd5GIIVrZFq2haZmoVeJ33 LJxo3RA5Tf9LoUeels1b4s9kFz6h7+AHERUpABEBAAG0IUNocmlzdGlhbiBCdWh0 eiA8YnVodHpAcG9zdGVvLmRlPokBPgQTAQIAKAUCVAiW4AIbAwUJAeEzgAYLCQgH AwIGFQgCCQoLBBYCAwECHgECF4AACgkQZLsXsAdRqOxNUAf/V/hDA5zGDpySuCEj DhjiVRK74J9Wd8gfH0WAf1Co5HZ24wZH8rgOIVIgXw8rWkOw/VA6xfdfT+64xjTY Fhkpbrk199nDzp72F7Jc4NC+x8xac2e3rK5ifSWhZx7L5A32pGYE+d16m3EEqImK D4gcZl38x9zdUnD4hHyXkIPz1uCfuMuGgWEnaUk4Wbj41CBZr3O0ABue6regV15U jaes8r+B8iCcY+0yP2kse+3iaCaMqNv5FgQZ9+b2Cql8pFkZJVtBVUw4GW3DWZJi du0O/YrC9TgS+xY9ht/MD2qSHwjcK1sdImjqBO7xP8TIOwKeYyDvGKnSO3EJ/sSA UPGEPrkBDQRUCJbgAQgA0k/Qg67CCUJE2/zuxBEoK4wLJpDRJzh8CQPZpjWx8VP0 KL892jwfxymXn8KNhuy1SgCBFSeV9jg4VZNWDlUGJc2lo82ajr9PzIsrQwu4lf0B zrUWV5hWepKu/kb8uSjx58YYfx0SFz4+9akX3Wwu9TUHntzL5Gk3Q26nnsr1xEJ+ VEumvCH9AE0Tk0K7dQpJ2/JcLuO+uhrpd/lHFDYVN5NsG3P015uFOkDI6N/xNFCj v95XNR93QlfKpK3qWlFGescfG+o/7Ub6s67/i/JoNbw0XgPEHmQfXpD7IHO4cu+p +ETb11cz+1mmi96cy98ID+uTiToJ8G//yD9rmtyxoQARAQABiQElBBgBAgAPBQJU CJbgAhsMBQkB4TOAAAoJEGS7F7AHUajs6sQH/iKs6sPc0vkRJLfbwrijZeecwCWF blo/jzIQ8jPykAj9SLjV20Xwqg3XcJyko8ZU6/zuRJq9xjlv9pZr/oVudQAt6v+h 2Cf4rKEjmau483wjMV2xjTXQhZi9+ttDbia4fgdmGtKsOicn5ae2fFXcXNPu3RiW sZKifWdokA6xqMW6iIG9YjjI5ShxngHWp2xfPscBFMDRtFOMags/Yx+YvwoyEZ4A dURYMFHFqpwILEc8hIzhRg1gq40AHbOaEdczS1Rr3T7/gS6eBs4u6HuY5g2Bierm lLjpspFPjMXwJAa/XLOBjMF2vsHPrZNcouNKkumQ36yq/Pm6DFXAseQDxOk= =PGP9 -END PGP PUBLIC KEY BLOCK- -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Created edge-case bug with `contains_eager`, can't reproduce
Ah ha! I figured this out. It was a mix of a lazy eye and some peculiarities between Postgres(live) and sqlite(test-case). Given the query: session.query(Foo).options(contains_eager('bar')).order_by(Foo.id.desc()).offset(0).limit(100).all()] The SQL is (approx) select foo.*, bar.* from foo, bar order by foo.id desc limit 100; I didn't notice that there wasn't a `join` from foo to bar until I reformatted the SQL. SqlAlchemy just drops in the 'bar' on the query. In such a case, shouldn't the library ideally: • raise some error or • not add in the "join" ? -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.