Re: [sqlalchemy] When to set "cache_ok" to True?

2021-05-26 Thread Jinghui Niu
Thank you. Could you please also give some hint on a case where we must set this attribute to `False`? On Wed, May 26, 2021 at 5:54 AM Mike Bayer wrote: > > > On Wed, May 26, 2021, at 3:07 AM, niuji...@gmail.com wrote: > > I have consistently receiving the warning: > will not produce a cache

Re: [sqlalchemy] conditionals inside column_property

2020-12-11 Thread Jinghui Niu
Thanks. One thing to clarify, I noticed that here you used `case` without using in a context of `select`. Is this considered a shorthand within sqlalchemy? On Fri, Dec 11, 2020 at 2:16 AM Simon King wrote: > You can do it, but you need to use an SQL conditional rather than a > python one. In

Re: [sqlalchemy] Why is this pattern discouraged in SQLAlchemy?

2017-08-07 Thread Jinghui Niu
Thanks very much for the informative reply Mike! On Sun, Aug 6, 2017 at 9:38 PM, Mike Bayer <clas...@zzzcomputing.com> wrote: > > > On Aug 6, 2017 8:11 PM, "Jinghui Niu" <niujing...@gmail.com> wrote: > > When reading the official SQLAlchemy docum

[sqlalchemy] Why is this pattern discouraged in SQLAlchemy?

2017-08-06 Thread Jinghui Niu
When reading the official SQLAlchemy documentation, I found the example below: ### this is the **wrong way to do it** ### class ThingOne(object): def go(self): session = Session() try: session.query(FooBar).update({"x": 5}) session.commit()

[sqlalchemy] Do I need to explicitly close each Session instance in a scoped_session context after use?

2017-08-06 Thread Jinghui Niu
I'm using Sqlite as backend, which doesn't use any connection pools. In my case, I don't know if there is any negative consequences to just let Python garbage collect each Session instance after use? Especially under a multi-threading context. Could someone point out any risks not closing them

[sqlalchemy] Re: How to get a Engine or Connection from Session?

2017-08-05 Thread Jinghui Niu
That's it! Thanks! On Saturday, August 5, 2017 at 3:15:28 PM UTC-7, Jonathan Vanasco wrote: > > > http://docs.sqlalchemy.org/en/latest/orm/session_api.html#sqlalchemy.orm.session.Session.connection > > > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

Re: [sqlalchemy] Re: How to get a Engine or Connection from Session?

2017-08-05 Thread Jinghui Niu
But it's not for returning a connection object? On Aug 5, 2017 1:51 PM, "Ruben Di Battista" <rubendibatti...@gmail.com> wrote: > session.bind? > > On Saturday, August 5, 2017 at 2:36:53 PM UTC+2, Jinghui Niu wrote: >> >> Is there a way to get the curren

[sqlalchemy] How to get a Engine or Connection from Session?

2017-08-05 Thread Jinghui Niu
Is there a way to get the currently binding Engine or Connection from a Session object? The backend database is Sqlite. Thanks. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and

Re: [sqlalchemy] Is there a way to configure session.commit() so that it automatically return the committed instance's class.__name__ + primary id?

2017-07-25 Thread Jinghui Niu
Thanks Simon. Your answers always help immensely getting to know SA better. I now know the overview. On Tue, Jul 25, 2017 at 1:45 AM, Simon King <si...@simonking.org.uk> wrote: > On Tue, Jul 25, 2017 at 2:35 AM, Jinghui Niu <niujing...@gmail.com> wrote: > > I was wonder

[sqlalchemy] Is there a way to configure session.commit() so that it automatically return the committed instance's class.__name__ + primary id?

2017-07-24 Thread Jinghui Niu
I was wondering if there is a way to configure Session.commit() so on each successful commit, it will return the committed/updated/deleted instance's class.__name__ + row.id. Is this possible? Thanks. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

Re: [sqlalchemy] How to better understand `remote_side` in sqlalchemy?

2017-07-20 Thread Jinghui Niu
Now I see, thanks Mike! On Jul 20, 2017 6:51 PM, "Mike Bayer" <mike...@zzzcomputing.com> wrote: > On Thu, Jul 20, 2017 at 8:10 PM, Jinghui Niu <niujing...@gmail.com> wrote: > > I see. But still I'm struggling to see the real difference between: > >

Re: [sqlalchemy] How to better understand `remote_side` in sqlalchemy?

2017-07-20 Thread Jinghui Niu
I see. But still I'm struggling to see the real difference between: Node.parent_id = Node.id vs. Node.id = Node.parent_id Aren't we just switching around sides here? On Thu, Jul 20, 2017 at 4:47 PM, Mike Bayer <mike...@zzzcomputing.com> wrote: > On Thu, Jul 20, 2017 at 1:34 PM, Ji

[sqlalchemy] How to better understand `remote_side` in sqlalchemy?

2017-07-20 Thread Jinghui Niu
I've been studying sqlalchemy's self referential table. I've read the documentation many times and still have difficulties understanding the concept of remote_side. Could someone please draw a diagram or use an analogy to help explain this concept? I think visualizing is a better way but

[sqlalchemy] Re: What is a best practice model for cache instances using their detached state?

2017-07-13 Thread Jinghui Niu
, July 13, 2017 at 4:38:19 PM UTC-7, Jinghui Niu wrote: > > I have a web application served by cherrypy (, which is multi-threaded. ) > > I'm trying to cache a set of rows queried from database using > `self.search_result_cache` variable on the GUI_Server object. On my > front-

Re: [sqlalchemy] What is a best practice model for cache instances using their detached state?

2017-07-13 Thread Jinghui Niu
Hi Mike, I've read the example of dogpile caching. For my case dogpile.cache seems to be a overkill, could you please provide a thinner example of using Query.merge_result without involving another library? Thanks. On Thu, Jul 13, 2017 at 8:07 PM, Jinghui Niu <niujing...@gmail.com>

Re: [sqlalchemy] What is a best practice model for cache instances using their detached state?

2017-07-13 Thread Jinghui Niu
hod that I've used in production successfully. > > > > > > On Thu, Jul 13, 2017 at 7:38 PM, Jinghui Niu <niujing...@gmail.com> wrote: > > I have a web application served by cherrypy (, which is multi-threaded. ) > > > > I'm trying to cache a set of rows queri

[sqlalchemy] What is a best practice model for cache instances using their detached state?

2017-07-13 Thread Jinghui Niu
I have a web application served by cherrypy (, which is multi-threaded. ) I'm trying to cache a set of rows queried from database using `self.search_result_cache` variable on the GUI_Server object. On my front-end, the web first request `list_entries` to prepare the rows and stores them on

Re: [sqlalchemy] How to query an optional column on a self referential adjacency list table?

2016-10-16 Thread Jinghui Niu
that leaves just MySQL out. > > On 10/16/2016 01:21 PM, Jinghui Niu wrote: > >> Thanks Mike. >> >> On Sun, Oct 16, 2016 at 7:14 AM, Mike Bayer <mike...@zzzcomputing.com >> <mailto:mike...@zzzcomputing.com>> wrote: >> >> >> >> On 10/

Re: [sqlalchemy] How to query an optional column on a self referential adjacency list table?

2016-10-16 Thread Jinghui Niu
Thanks Mike. On Sun, Oct 16, 2016 at 7:14 AM, Mike Bayer <mike...@zzzcomputing.com> wrote: > > > On 10/14/2016 06:08 PM, Jinghui Niu wrote: > >> I have the following Table model representing a timeline. >> >> | >> classTimeRange(Bas

[sqlalchemy] Re: How to query an optional column on a self referential adjacency list table?

2016-10-14 Thread Jinghui Niu
14, 2016 at 3:08:32 PM UTC-7, Jinghui Niu wrote: > > I have the following Table model representing a timeline. > > class TimeRange(Base): > > > __tablename__ = "time_line" > > > record_id = Column(Integer, primary_key=True) > level = Column(Str

[sqlalchemy] How to query an optional column on a self referential adjacency list table?

2016-10-14 Thread Jinghui Niu
I have the following Table model representing a timeline. class TimeRange(Base): __tablename__ = "time_line" record_id = Column(Integer, primary_key=True) level = Column(String, nullable=False) # e.g. "Point", "Range" content = Column(String, nullable=False)

Re: [sqlalchemy] How to implement SQL level expression for this hybrid property?

2016-10-03 Thread Jinghui Niu
t; parameter is now the > *class*, so self.firstname and self.lastname are SQLAlchemy column > properties. Since SA implements the "+" operator for those properties, > the result of the expression is an SQL expression. When you write > "User.fullname == 'Jinghui

Re: [sqlalchemy] How to implement SQL level expression for this hybrid property?

2016-10-03 Thread Jinghui Niu
nderstood your comment here? On Mon, Oct 3, 2016 at 1:40 AM, Jinghui Niu <niujing...@gmail.com> wrote: > Thank you Simon. Your explanation helps me understand this quite a lot. > Sometimes the documentation is so terse that only when you fully understand > the subject then you can

Re: [sqlalchemy] How to implement SQL level expression for this hybrid property?

2016-10-03 Thread Jinghui Niu
suggest modify my current code? Or maybe you could please point out a link to where I can explore further on the python to SQL transition? Thank you so much. Jinghui On Mon, Oct 3, 2016 at 1:27 AM, Simon King <si...@simonking.org.uk> wrote: > On Mon, Oct 3, 2016 at 7:17 AM, Jinghui Niu

[sqlalchemy] How to implement SQL level expression for this hybrid property?

2016-10-03 Thread Jinghui Niu
I have a ledger table and a corresponding python class. I defined the model using SQLAlchemy, as follows, class Ledger(Base): __tablename__ = 'ledger' currency_exchange_rate_lookup = {('CNY', 'CAD'): 0.2} amount = Column(Numeric(10, 2), nullable=False) currency =

[sqlalchemy] How can I programmatically give a hybrid_property its name?

2016-09-28 Thread Jinghui Niu
The documentation shows that hybrid_property should used as a decorator, like: @hybrid_property def my_property(self): pass What if I wanted to give this hybrid property a name by referring a variable in runtime? Is this allowed? Thanks. -- You received this message because you are

[sqlalchemy] How can I duplicate mixin columns?

2016-09-16 Thread Jinghui Niu
For example, I have a Mixin as follow: class MyNoteMixin: note = Column(String) Now I have a subclass that inherit from the above Mixin, but needs two different columns both are of a note nature. Can I do something like: class Child(Base, MyNoteMixin as "Description", MyNoteMixin as

Re: [sqlalchemy] SQL expression for function (and/or method) dispatching?

2016-09-16 Thread Jinghui Niu
DATE(timepoint, ), > STR_TO_DATE(timepoint, )) > > If you're not actually going to filter on these columns, I don't think > I'd do any of this at all. Instead, I'd use something like a > TypeDecorator to convert the values when loading from and saving to > the database. > >

Re: [sqlalchemy] SQL expression for function (and/or method) dispatching?

2016-09-16 Thread Jinghui Niu
my database. I admit that it's one of my personal preference though:) On Fri, Sep 16, 2016 at 1:49 AM, Jinghui Niu <niujing...@gmail.com> wrote: > Thanks for reply. > > The reason is simple. I plan in the future to accommodate datetime range > into that column as well, so sto

Re: [sqlalchemy] SQL expression for function (and/or method) dispatching?

2016-09-16 Thread Jinghui Niu
aps with a second column to indicate whether or not the > time part is valid)? > > Simon > > On Fri, Sep 16, 2016 at 4:39 AM, Jinghui Niu <niujing...@gmail.com> wrote: > > I have the following code snippet, I marked my question in a comment line > > inside the h

[sqlalchemy] SQL expression for function (and/or method) dispatching?

2016-09-15 Thread Jinghui Niu
I have the following code snippet, I marked my question in a comment line inside the hybrid_property.expression part. As you can see, it is now not implemented: from sqlalchemy.ext.declarative import declared_attr from sqlalchemy import Column, Integer, String, Unicode, UnicodeText from

[sqlalchemy] hybrid_property vs. hybrid_method, other than the latter can take arguments?

2016-08-23 Thread Jinghui Niu
Hi, I wonder if there is any recommendation or best practice on choosing between hybrid_property and hybrid_method , other than they hybrid_method can take arguments? If I use the hybrid_method only throughout, without giving it a argument more than self, doesn't it equal to using

Re: [sqlalchemy] What criteria should I use to determine whether I need a distinct class-level expression for my hybrid attributes?

2016-08-23 Thread Jinghui Niu
Thanks Mike. In future is it likely to have the instance level expression and class level expression automatically translated by ORM? It will be so much easier! On Mon, Aug 22, 2016 at 7:20 AM, Mike Bayer <mike...@zzzcomputing.com> wrote: > > > On 08/22/2016 03:20 AM, Jinghui Niu

[sqlalchemy] What criteria should I use to determine whether I need a distinct class-level expression for my hybrid attributes?

2016-08-22 Thread Jinghui Niu
I'm creating a mixin for my timestamp columns throughout my projects. Internally, the mixin uses UTC strings to store timestamps, externally it converts back and forth into local time using a second column that stores timezone information. I'm studying the hybrid attribute section in the

[sqlalchemy] thread-safety of Session, Engine, and Connections

2015-08-16 Thread Jinghui Niu
For clarity I made a table below in my notes out of reading the documentation and generalize the points: Engine: no worries, Engine object is always thread safe. Session: Session objects are tricky, they can be quite dangerous to use in a multi-threading context if one is not careful. Forget

Re: [sqlalchemy] Does twophase=True limit to only two databases at the same time?

2015-08-14 Thread Jinghui Niu
Oh by the way, I'm using SQLite as backend. On Aug 14, 2015 2:04 AM, Jinghui Niu niujing...@gmail.com wrote: I have three different DBs, one is person.db, another is journal.db, yet another is tag.db. In the documentation it reads: Vertical partitioning places different kinds of objects

[sqlalchemy] Does twophase=True limit to only two databases at the same time?

2015-08-14 Thread Jinghui Niu
I have three different DBs, one is person.db, another is journal.db, yet another is tag.db. In the documentation it reads: Vertical partitioning places different kinds of objects, or different tables, across multiple databases: engine1 = create_engine('postgresql://db1') engine2 =

[sqlalchemy] Re: Does twophase=True limit to only two databases at the same time?

2015-08-14 Thread Jinghui Niu
Thanks for all these helpful feedback. If I still want to use SQLite, and I still need to do vertical partition, what can I do? Am I out of luck? On Friday, August 14, 2015 at 2:04:37 AM UTC-7, Jinghui Niu wrote: I have three different DBs, one is person.db, another is journal.db, yet

[sqlalchemy] Re: Does twophase=True limit to only two databases at the same time?

2015-08-14 Thread Jinghui Niu
reference. I suppose such feature is relatively commonly needed among SQLite users isn't it? On Friday, August 14, 2015 at 3:48:40 PM UTC-7, Jonathan Vanasco wrote: On Friday, August 14, 2015 at 5:16:48 PM UTC-4, Jinghui Niu wrote: If I still want to use SQLite, and I still need to do

[sqlalchemy] Re: Does twophase=True limit to only two databases at the same time?

2015-08-14 Thread Jinghui Niu
Thanks Jonathan for pointing out the direction, it is very helpful to know where I can find more info. On Friday, August 14, 2015 at 5:06:09 PM UTC-7, Jonathan Vanasco wrote: Well, this problem doesn't really have anything to do with SqlAlchemy -- you should probably ask people for advice on

[sqlalchemy] Re: Does twophase=True limit to only two databases at the same time?

2015-08-14 Thread Jinghui Niu
:07 PM UTC-7, Jinghui Niu wrote: Thanks Jonathan for pointing out the direction, it is very helpful to know where I can find more info. On Friday, August 14, 2015 at 5:06:09 PM UTC-7, Jonathan Vanasco wrote: Well, this problem doesn't really have anything to do with SqlAlchemy -- you should

[sqlalchemy] Session.merge() use case (possible) ambiguity

2015-08-12 Thread Jinghui Niu
About the Session.merge(), the documentation gives several examples of use cases, I think there might be a slight ambiguity in this one: An application which reads an object structure from a file and wishes to save it to the database might parse the file, build up the structure, and then use

[sqlalchemy] What exactly is the rationale behind manage the life cycle of the session externally to functions that deal with specific data.?

2015-08-11 Thread Jinghui Niu
In the Documentation -- Session Basics, I read this: E.g. don’t do this: ### this is the **wrong way to do it** ### class ThingOne(object): def go(self): session = Session() try: session.query(FooBar).update({x: 5}) session.commit()

Re: [sqlalchemy] SQLite: How can I turn on ON DELETE CASCADE from within sqlalchemy?

2015-08-06 Thread Jinghui Niu
/15 5:31 PM, Jinghui Niu wrote: I know you can set this constraint if you are directly dealing with sqlite3, but how can I achieve this database level setting from within sqlalchemy? The documentation reads: Note that these clauses are not supported on SQLite, and require InnoDB tables

[sqlalchemy] SQLite: How can I turn on ON DELETE CASCADE from within sqlalchemy?

2015-08-06 Thread Jinghui Niu
I know you can set this constraint if you are directly dealing with sqlite3, but how can I achieve this database level setting from within sqlalchemy? The documentation reads: Note that these clauses are not supported on SQLite, and require InnoDB tables when used with MySQL. They may also

[sqlalchemy] Where can I find the signature definition for @validates functions?

2015-08-06 Thread Jinghui Niu
I read the documentation and encountered this: @validates('addresses', include_backrefs=False) def validate_address(self, key, address): assert '@' in address.email return address What are the key and address in the validate_address function? I can't find any explanation in our

[sqlalchemy] searching for multiple ( unknown mumber) keywords

2015-07-31 Thread Jinghui Niu
for example, I'd like to search for keywords foo, bar and possibly but not necessarily more keywords in the description column of my *Item* class, so I build a list of such keywords: [foo, bar], and then use primitive looping to achieve my goal: query_obj = session.query(Item) for k in

Re: [sqlalchemy] Re: what is the way to achieve automatic conversion on each query?

2015-07-26 Thread Jinghui Niu
property method, I didn't quite understand it back then, does it relate to the two techniques you suggested here? On Sun, Jul 26, 2015 at 11:06 AM, Mike Bayer mike...@zzzcomputing.com wrote: On 7/25/15 6:38 PM, Jinghui Niu wrote: By the way, the database driver that I'm using is SQLite, which