Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQLAlchemy, how do I define an event to fire DDL using declarative syntax?

This example shows how to use it with "non-declarative" - http://docs.sqlalchemy.org/en/latest/core/ddl.html#sqlalchemy.schema.DDL

How can I use it with the ORM declarative syntax?

For example, with this structure:

Base = declarative_base(bind=engine)     
class TableXYZ(Base):
    __tablename__ = 'tablexyz'
like image 714
Richard Green Avatar asked Aug 20 '12 13:08

Richard Green


People also ask

What is Declarative in SQLAlchemy?

The Declarative system is the typically used system provided by the SQLAlchemy ORM in order to define classes mapped to relational database tables. However, as noted in Classical Mappings, Declarative is in fact a series of extensions that ride on top of the SQLAlchemy mapper() construct.

How do you define SQLAlchemy?

SQLAlchemy is a library that facilitates the communication between Python programs and databases. Most of the times, this library is used as an Object Relational Mapper (ORM) tool that translates Python classes to tables on relational databases and automatically converts function calls to SQL statements.

What is __ Table_args __?

Table arguments other than the name, metadata, and mapped Column arguments are specified using the __table_args__ class attribute. This attribute accommodates both positional as well as keyword arguments that are normally sent to the Table constructor. The attribute can be specified in one of two forms.


2 Answers

Silly example, but think this is what you're looking for, should get you going:

from sqlalchemy import event
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import create_session
from sqlalchemy.schema import Column, DDL
from sqlalchemy.types import Integer

Base = declarative_base()
engine = create_engine('sqlite:////tmp/test.db', echo=True)

class TableXYZ(Base):
    __tablename__ = 'tablexyz'
    id = Column(Integer, primary_key=True)

#event.listen(
#   Base.metadata, 'after_create',
#   DDL("""
#   alter table TableXYZ add column name text
#   """)

event.listen(
    TableXYZ.__table__, 'after_create',
    DDL("""
    alter table TableXYZ add column name text
    """)
)
Base.metadata.create_all(engine)

Running the above results in - note "name text" for the added column:

sqlite> .schema tablexyz
CREATE TABLE tablexyz (
    id INTEGER NOT NULL, name text, 
    PRIMARY KEY (id)
);

I have my code in declarative and use the event.listen to add triggers and other stored procedures. Seems to work well.

like image 53
Tony Gibbs Avatar answered Oct 04 '22 18:10

Tony Gibbs


It should be the same with "non-declarative" and "declarative".

You register your event by specifying (with your class and the doc's event & function) :

event.listen(TableXYZ, 'before_create', DDL('DROP TRIGGER users_trigger'))

Syntax is something like:

event.listen(Class, 'name_of_event', function)
like image 24
b4stien Avatar answered Oct 04 '22 16:10

b4stien