Re: [sqlalchemy] session.execute() list of params with nullable fields
I understood, but it's not obvious, for me. Is it mentioned in the docs? On Monday, January 19, 2015 at 4:47:18 PM UTC+3, Michael Bayer wrote: the first entry in the list of parameters determines how the INSERT statement will be written. if you have different sets of keys in each parameter set, then you should invoke session.execute() individually for each set of parameters. Pavel Aborilov abor...@gmail.com javascript: wrote: Hi! I have model with nullable fields and try to add bulk of items: insert = model.__table__.insert() session.execute(insert, events) where events in the list of dicts with params and if first element in list don't have that nullable params, then event if other elements have one, they won't be added. Where I am wrong? -- 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+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.com javascript:. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] session.execute() list of params with nullable fields
Hi! I have model with nullable fields and try to add bulk of items: insert = model.__table__.insert() session.execute(insert, events) where events in the list of dicts with params and if first element in list don't have that nullable params, then event if other elements have one, they won't be added. Where I am wrong? -- 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] session.execute() list of params with nullable fields
Oh, sorry, didn't find that. пн, 19 янв. 2015, 19:06, Michael Bayer mike...@zzzcomputing.com: yes: http://docs.sqlalchemy.org/en/rel_0_9/core/tutorial.html# executing-multiple-statements When executing multiple sets of parameters, each dictionary must have the same set of keys; i.e. you cant have fewer keys in some dictionaries than others. This is because the Insert statement is compiled against the first dictionary in the list, and it’s assumed that all subsequent argument dictionaries are compatible with that statement.” Pavel Aborilov abori...@gmail.com wrote: I understood, but it's not obvious, for me. Is it mentioned in the docs? On Monday, January 19, 2015 at 4:47:18 PM UTC+3, Michael Bayer wrote: the first entry in the list of parameters determines how the INSERT statement will be written. if you have different sets of keys in each parameter set, then you should invoke session.execute() individually for each set of parameters. Pavel Aborilov abor...@gmail.com wrote: Hi! I have model with nullable fields and try to add bulk of items: insert = model.__table__.insert() session.execute(insert, events) where events in the list of dicts with params and if first element in list don't have that nullable params, then event if other elements have one, they won't be added. Where I am wrong? -- 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+...@googlegroups.com. To post to this group, send email to sqlal...@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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. -- You received this message because you are subscribed to a topic in the Google Groups sqlalchemy group. To unsubscribe from this topic, visit https://groups.google.com/d/ topic/sqlalchemy/ScGtudTx9U8/unsubscribe. To unsubscribe from this group and all its topics, 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. -- 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] Group delete with inheritance
thanks a lot for your answer On Wednesday, July 16, 2014 5:48:29 PM UTC+4, Michael Bayer wrote: On Jul 16, 2014, at 1:29 AM, Pavel Aborilov abor...@gmail.com javascript: wrote: Hi! I have two models class DB_Object(Base): __tablename__ = 'objects' id = Column(Integer, primary_key=True) name = Column(String(50), unique=True, nullable=False) type = Column(String(50), default=object) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Contact(DB_Object): __tablename__ = 'contacts' id = Column(Integer, ForeignKey('objects.id'), primary_key=True) enable = Column(Boolean, default=False) normal_open = Column(Boolean, default=True) cr_enabled = Column(Boolean, default=False) __mapper_args__ = {'polymorphic_identity': 'contact'} I need to delete group of contacts by id, but if I do ids = (1,2,3) session.query(Contact).filter(Contact.id.in_(ids)).delete(synchronize_session=fetch) it's remove only contacts and leave object in objects table. If i remove like this: o = session.query(Contact).get(id) session.delete(o) it remove objects from both tables. How can I do this for group of id? sorry, I read that completely wrong. Inheritance. Yeah, again query.delete() can’t work for an inherited subclass like that. You’d need to DELETE on the base table and again ON DELETE CASCADE would need to accommodate the child table. -- 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] Group delete with inheritance
Hi! I have two models class DB_Object(Base): __tablename__ = 'objects' id = Column(Integer, primary_key=True) name = Column(String(50), unique=True, nullable=False) type = Column(String(50), default=object) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Contact(DB_Object): __tablename__ = 'contacts' id = Column(Integer, ForeignKey('objects.id'), primary_key=True) enable = Column(Boolean, default=False) normal_open = Column(Boolean, default=True) cr_enabled = Column(Boolean, default=False) __mapper_args__ = {'polymorphic_identity': 'contact'} I need to delete group of contacts by id, but if I do ids = (1,2,3) session.query(Contact).filter(Contact.id.in_(ids)).delete(synchronize_session=fetch) it's remove only contacts and leave object in objects table. If i remove like this: o = session.query(Contact).get(id) session.delete(o) it remove objects from both tables. How can I do this for group of id? -- 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] Override column in child with polymorphic
You right, I dont have name column in commands table, thats why If I do def __init__(self, **kw): kw.setdefault(“name”, “Command”) super(Command, self).__init__(**kw) name = column_property(Column(String(50), default=“Command”), DB_Object.name) I have: table commands has no column named. I dont need this column in Command, but in Command I need to set name of DB_Object to value that depends on other Command columns. How can I do this? On Friday, May 23, 2014 12:51:14 AM UTC+4, Michael Bayer wrote: On May 22, 2014, at 7:42 AM, Pavel Aborilov abor...@gmail.comjavascript: wrote: Hi! How can I set default for parent field? class DB_Object(Base): __tablename__ = 'objects' id = Column(Integer, primary_key=True) name = Column(String(50), unique=True, nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Command(DB_Object): __tablename__ = commands id = Column(Integer, ForeignKey('objects.id'), primary_key=True) name = Column(String(50), default=Command) If I try to commit Command object I have objects.name may not be NULL” This is all assuming that “name” is the same value on both DB_Object and on Command. Since Command extends from DB_Object, you’d want to remove the “name “column from the “commands” table - it is redundant vs. the “name” column that’s on “objects”. Alternatively, you can map both “name” columns on Command as follows: class Command(DB_Object): # … name = column_property(Column(String(50), default=“Command”), DB_Object.name) However, in either case, you will also need to establish that “default” value in the constructor (or in the init event, see http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=init%20event#sqlalchemy.orm.events.InstanceEvents.init) , so that it applies to both “name” columns during flush: class Command(DB_Object): # … def __init__(self, **kw): kw.setdefault(“name”, “Command”) super(Command, self).__init__(**kw) -- 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+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.comjavascript: . Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] Override column in child with polymorphic
I think I need to generate value of that field before commit, some how. On Friday, May 23, 2014 10:55:31 AM UTC+4, Pavel Aborilov wrote: You right, I dont have name column in commands table, thats why If I do def __init__(self, **kw): kw.setdefault(“name”, “Command”) super(Command, self).__init__(**kw) name = column_property(Column(String(50), default=“Command”), DB_Object.name) I have: table commands has no column named. I dont need this column in Command, but in Command I need to set name of DB_Object to value that depends on other Command columns. How can I do this? On Friday, May 23, 2014 12:51:14 AM UTC+4, Michael Bayer wrote: On May 22, 2014, at 7:42 AM, Pavel Aborilov abor...@gmail.com wrote: Hi! How can I set default for parent field? class DB_Object(Base): __tablename__ = 'objects' id = Column(Integer, primary_key=True) name = Column(String(50), unique=True, nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Command(DB_Object): __tablename__ = commands id = Column(Integer, ForeignKey('objects.id'), primary_key=True) name = Column(String(50), default=Command) If I try to commit Command object I have objects.name may not be NULL” This is all assuming that “name” is the same value on both DB_Object and on Command. Since Command extends from DB_Object, you’d want to remove the “name “column from the “commands” table - it is redundant vs. the “name” column that’s on “objects”. Alternatively, you can map both “name” columns on Command as follows: class Command(DB_Object): # … name = column_property(Column(String(50), default=“Command”), DB_Object.name) However, in either case, you will also need to establish that “default” value in the constructor (or in the init event, see http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=init%20event#sqlalchemy.orm.events.InstanceEvents.init) , so that it applies to both “name” columns during flush: class Command(DB_Object): # … def __init__(self, **kw): kw.setdefault(“name”, “Command”) super(Command, self).__init__(**kw) -- 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+...@googlegroups.com. To post to this group, send email to sqlal...@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] Override column in child with polymorphic
Hi! How can I set default for parent field? class DB_Object(Base): __tablename__ = 'objects' id = Column(Integer, primary_key=True) name = Column(String(50), unique=True, nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Command(DB_Object): __tablename__ = commands id = Column(Integer, ForeignKey('objects.id'), primary_key=True) name = Column(String(50), default=Command) If I try to commit Command object I have objects.name may not be NULL -- 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] dogpile with SA inheritance
Hello! How can I cache query like this: session.query(Person).get(51) where 51 is id of Man I can't access attribute age of Man without SELECT. Models: class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Man(Person): __tablename__ = 'man' id = Column(Integer, ForeignKey('person.id'), primary_key=True) age = Column(String(100), nullable=False) __mapper_args__ = {'polymorphic_identity': 'man'} -- 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] dogpile with SA inheritance
but I dont know on the time of query what the type of object it will be. On Tuesday, April 15, 2014 5:06:48 PM UTC+4, Gunnlaugur Briem wrote: Hi Pavel, You want: s.query(Person).with_polymorphic(Man).get(51) Cheers, Gulli On Tue, Apr 15, 2014 at 12:59 PM, Pavel Aborilov abor...@gmail.comjavascript: wrote: Hello! How can I cache query like this: session.query(Person).get(51) where 51 is id of Man I can't access attribute age of Man without SELECT. Models: class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Man(Person): __tablename__ = 'man' id = Column(Integer, ForeignKey('person.id'), primary_key=True) age = Column(String(100), nullable=False) __mapper_args__ = {'polymorphic_identity': 'man'} -- 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+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.comjavascript: . Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] dogpile with SA inheritance
There is a lot of them. But I think it have to be simple. Like a normal query. From the docs I understand how to cache query with FromCache, how to cache relationship with RelationshipCache, but I don't find how to cache polymorphic relationships. On Tuesday, April 15, 2014 5:56:36 PM UTC+4, Gunnlaugur Briem wrote: On Tue, Apr 15, 2014 at 1:11 PM, Pavel Aborilov abor...@gmail.comjavascript: wrote: but I dont know on the time of query what the type of object it will be. Then you can use session.query(Person).with_polymorphic('*') to mean joining to the tables of all mapped subclasses. (Be aware that this can become problematic if there is a lot of them.) Cheers, Gulli On Tuesday, April 15, 2014 5:06:48 PM UTC+4, Gunnlaugur Briem wrote: Hi Pavel, You want: s.query(Person).with_polymorphic(Man).get(51) Cheers, Gulli On Tue, Apr 15, 2014 at 12:59 PM, Pavel Aborilov abor...@gmail.comwrote: Hello! How can I cache query like this: session.query(Person).get(51) where 51 is id of Man I can't access attribute age of Man without SELECT. Models: class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) name = Column(String(100), nullable=False) type = Column(String(50)) __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': type } class Man(Person): __tablename__ = 'man' id = Column(Integer, ForeignKey('person.id'), primary_key=True) age = Column(String(100), nullable=False) __mapper_args__ = {'polymorphic_identity': 'man'} -- 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+...@googlegroups.com. To post to this group, send email to sqlal...@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.comjavascript: . Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] Re: Twisted + SQLAlchemy
In Twisted there is a great thing known as `ContextTracker`: provides a way to pass arbitrary key/value data up and down a call stack without passing them as parameters to the functions on that call stack. In my twisted web app in method `render_GET` I set a `uuid` parameter: call = context.call({uuid: str(uuid.uuid4())}, self._render, request) and then I call the `_render` method to do the actual work (work with db, render html, etc). I create the `scoped_session` like this: scopefunc = functools.partial(context.get, uuid) Session = scoped_session(session_factory, scopefunc=scopefunc) Now within any function calls of `_render` I can get session with: Session() and at the end of `_render` I have to do `Session.remove()` to remove the session. It works with my webapp and I think can work for other tasks. This is completely standalone example, show how all it work together. from twisted.internet import reactor, threads from twisted.web.resource import Resource from twisted.web.server import Site, NOT_DONE_YET from twisted.python import context from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.ext.declarative import declarative_base import uuid import functools engine = create_engine( 'sqlite:///test.sql', connect_args={'check_same_thread': False}, echo=False) session_factory = sessionmaker(bind=engine) scopefunc = functools.partial(context.get, uuid) Session = scoped_session(session_factory, scopefunc=scopefunc) Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) Base.metadata.create_all(bind=engine) class TestPage(Resource): isLeaf = True def render_GET(self, request): context.call({uuid: str(uuid.uuid4())}, self._render, request) return NOT_DONE_YET def render_POST(self, request): return self.render_GET(request) def work_with_db(self): user = User(name=TestUser) Session.add(user) Session.commit() return user def _render(self, request): print session: , id(Session()) d = threads.deferToThread(self.work_with_db) def success(result): html = added user with name - %s % result.name request.write(html.encode('UTF-8')) request.finish() Session.remove() call = functools.partial(context.call, {uuid: scopefunc()}, success) d.addBoth(call) return d if __name__ == __main__: reactor.listenTCP(, Site(TestPage())) reactor.run() Still I dont know how to make it work with `defer.inLineCallback`, that I use everywhere in my code. -- 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/groups/opt_out.
[sqlalchemy] Re: Twisted + SQLAlchemy
scoped_session use threads only if you dont use scopefunc. On Saturday, February 15, 2014 2:05:33 AM UTC+4, Jonathan Vanasco wrote: I don't know if any of this will help - I'm too tired to read though all your code: 1. Twisted isn't threadsafe, so anything happening in deferToThread needs to be entirely encapsulated. This has totally screwed me up many times. You should avoid a scoped_session , as it has to do with threads. 2. You can't expect anything in twisted to be reused. The safest way i've found, and I've only experimented on migrating my twisted work to sqlalchemy : 1. Use a regular session for each request and explicitly pass it around. never use scoped_session. 2. Avoid threaded stuff as much as possible. For a while I had a `deferToThread` that wrapped a `runInteraction` . That ended up causing a memory leak ( threads spawning threads ). someone on this list was one trying to get scoped_session to work with twisted, and there was a lot of code that he wrote to do it. there was never an official update. -- 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/groups/opt_out.
[sqlalchemy] Re: Twisted + SQLAlchemy
now, I'm using it like this: @defer.inlineCallbacks def jsonrpc_get(self, id): self.log.debug(get device with id %s % id) device = yield deferToThread(self.devices.get, id) // then generate output from device, where device is ORM object if devices.get method I use scoped_session. But I can't remove at the end of method get, because it return ORM object and if I remove sessoin then I wont have accecc to device fields. I can close this session at the end of the jsonrpc_get: session = object_session(device) session.close() but I cant remove it with Session.remove() because I already in an other thread. In next request I will have the same session object(if twisted give out the same thread from threadpool) Is it normal to close session and dont remove it? On Thursday, February 13, 2014 10:02:54 AM UTC+4, Pavel Aborilov wrote: Hello! I'm working with SA from Twisted, and it's not so simple as I though. I red some old topic in this group and didn't find any good solution. I just want to know if there are any approach at present? -- 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/groups/opt_out.
[sqlalchemy] Twisted + SQLAlchemy
Hello! I'm working with SA from Twisted, and it's not so simple as I though. I red some old topic in this group and didn't find any good solution. I just want to know if there are any approach at present? -- 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/groups/opt_out.
Re: [sqlalchemy] What is the best way to store runtime information in model?
Is it bad to use one session within app and never close it? On Friday, January 31, 2014 9:04:01 AM UTC+4, Pavel Aborilov wrote: I need to have access to this state in a whole life of app, but as I undestand it's not a good way to use one session all time. Proper way, to open session, do all my stuff with DB, then close session. But then I lost my state. In java I have DAO layer and business object, that store all my db field and all my states regardless of session. but with SA I already have session, DBO object and Manager object. I dont want to create so much layers, I think its not much pythonic. On Friday, January 31, 2014 12:51:41 AM UTC+4, Michael Bayer wrote: On Jan 30, 2014, at 1:58 PM, Pavel Aborilov abor...@gmail.com wrote: Hi! What is the best way to store runtime information in model? attributes and columns have an .info property you can use, if this is per-attribute User.fullname.info[‘some_info’] = ‘bar’ otherwise certainly, store any additional state on your object as needed, it’s a regular Python object, “self._online = 0”, sure, thats great If I get object from session like user = session.query(User).get(1) change state user.online = 1 and after session.close() I have detached object Do I always have to do expunge(user) after commit() and before close() you never need to use expunge() and generally the Session is mostly intended to be in progress when you work with your objects. when you call .close(), you should be done using all your objects - they’d either be gone, or stored away in some kind of cache or something if you’re moving them to another Session. basically if you use the session as it is in the ORM tutorial, that’s the main way to use it. The Session is always there when you’re using objects. Is there any other ways? all kinds but you need to be more aware of object lifecycle if you’re coming up with your own system. what is the most used practice, to create DAO layer or session it self work like DAO layer? the Session itself is probably not suitable as a *large* scale DAO, for simple things sure, but if your app has lots of complex use cases then its better to have functions that represent those specific use cases directly. -- 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/groups/opt_out.
[sqlalchemy] What is the best way to store runtime information in model?
Hi! What is the best way to store runtime information in model? And is it good idea to store one in a model(like online/offline, etc) for example: class User(Base):__tablename__ = 'users'id = Column(Integer, primary_key=True)username = Column(String, unique=True, nullable=False) fullname = Column(String, default='')password = Column(String, nullable=False)role = Column(String, nullable=False) status = {0: Offline, 1: Online, -1: Unknown} def __init__(self, **kwargs): Base.__init__(self, **kwargs)self.init_status() @orm.reconstructordef init_status(self):self._online = 0 @propertydef online(self):if self._online is None: self._online = 0if self.enable:return self._online return -1@online.setterdef online(self, value):if value != self.online:dispatcher.send(sender=self, signal=state, value=value)self._online = value If I get object from session like user = session.query(User).get(1) change state user.online = 1 and after session.close() I have detached object Do I always have to do expunge(user) after commit() and before close() and then if I want to change it, I have to add it to new session and the again commit,expunge,close Is there any other ways? P.S. what is the most used practice, to create DAO layer or session it self work like DAO layer? Thanks. -- 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/groups/opt_out.
Re: [sqlalchemy] What is the best way to store runtime information in model?
I need to have access to this state in a whole life of app, but as I undestand it's not a good way to use one session all time. Proper way, to open session, do all my stuff with DB, then close session. But then I lost my state. In java I have DAO layer and business object, that store all my db field and all my states regardless of session. but with SA I already have session, DBO object and Manager object. I dont want to create so much layers, I think its not much pythonic. On Friday, January 31, 2014 12:51:41 AM UTC+4, Michael Bayer wrote: On Jan 30, 2014, at 1:58 PM, Pavel Aborilov abor...@gmail.comjavascript: wrote: Hi! What is the best way to store runtime information in model? attributes and columns have an .info property you can use, if this is per-attribute User.fullname.info[‘some_info’] = ‘bar’ otherwise certainly, store any additional state on your object as needed, it’s a regular Python object, “self._online = 0”, sure, thats great If I get object from session like user = session.query(User).get(1) change state user.online = 1 and after session.close() I have detached object Do I always have to do expunge(user) after commit() and before close() you never need to use expunge() and generally the Session is mostly intended to be in progress when you work with your objects. when you call .close(), you should be done using all your objects - they’d either be gone, or stored away in some kind of cache or something if you’re moving them to another Session. basically if you use the session as it is in the ORM tutorial, that’s the main way to use it. The Session is always there when you’re using objects. Is there any other ways? all kinds but you need to be more aware of object lifecycle if you’re coming up with your own system. what is the most used practice, to create DAO layer or session it self work like DAO layer? the Session itself is probably not suitable as a *large* scale DAO, for simple things sure, but if your app has lots of complex use cases then its better to have functions that represent those specific use cases directly. -- 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/groups/opt_out.