Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically add a SQLAlchemy object to the session?

I have a SQLAlchemy table class created using the Declarative method:

mysqlengine = create_engine(dsn)
session = scoped_session(sessionmaker(bind=mysqlengine))
Base = declarative_base()
Base.metadata.bind = mysqlengine

class MyTable(Base):
  __table_args__ = {'autoload' : True}

Now, when using this table within the code I would like to not have to use the session.add method in order to add each new record to the active session so instead of:

row = MyTable(1, 2, 3)
session.add(row)
session.commit()

I would like to have:

row = MyTable(1, 2, 3)
session.commit()

Now, I know of this question already: Possible to add an object to SQLAlchemy session without explicit session.add()?

And, I realize you can force this behavior by doing the following:

class MyTable(Base):
  def __init__(self, *args, **kw):
    super(MyTable, self).__init__(*args, **kw)
    session.add(self)

However, I do not want to bloat my code containing 30 tables with this method. I also know that Elixir ( http://elixir.ematia.de/trac/wiki ) does this so it must be possible in some sense.

like image 461
Mike Avatar asked Mar 26 '12 13:03

Mike


1 Answers

Super simple. Use an event:

from sqlalchemy import event, Integer, Column, String
from sqlalchemy.orm import scoped_session, sessionmaker, mapper
from sqlalchemy.ext.declarative import declarative_base

Session = scoped_session(sessionmaker())

@event.listens_for(mapper, 'init')
def auto_add(target, args, kwargs):
    Session.add(target)

Base = declarative_base()

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    data = Column(String)

a1 = A(data="foo")
assert a1 in Session()
like image 67
zzzeek Avatar answered Sep 22 '22 02:09

zzzeek