sqlalchemy

SQLAlchemy: avoiding repetition in declarative style class definition

I'm using SQLAlchemy, and many classes in my object model have the same two attributes: id and (integer & primary key), and name (a string). I'm trying to avoid declaring them in every class like so: class C1(declarative_base()): id = Column(Integer, primary_key = True) name = Column(String) #... class C2(declarative_base()...

sqlalchemy: turn off declarative polymorphic join?

Is there a way in sqlalchemy to turn off declarative's polymorphic join loading, in a single query? Most of the time it's nice, but I have: class A(Base) : discriminator = Column('type', mysql.INTEGER(1), index=True, nullable=False) __mapper_args__ = { 'polymorphic_on' : discriminator } id = Column(Integer, primary_key=True) ...

How can I use multiple databases in the same request in Cherrypy and SQLAlchemy?

Hey SO'ers, My app connects to multiple databases using a technique similar to this. It works so long as I don't try to access different databases in the same request. Having looked back to the above script I see they have written a comment to this end: SQLAlchemy integration for CherryPy, such that you can access multiple databases, b...

How to write a common get_by_id() method for all kinds of models in Sqlalchemy?

I'm using pylons with sqlalchemy. I have several models, and found myself wrote such code again and again: question = Session.query(Question).filter_by(id=question_id).one() answer = Session.query(Answer).fileter_by(id=answer_id).one() ... user = Session.query(User).filter_by(id=user_id).one() Since the models are all extend class Bas...

How to add event callbacks to sqlalchemy models?

There is a page demonstrates how to do it when using Elixir: http://beachcoder.wordpress.com/2007/05/02/adding-event-callbacks-to-sqlalchemyelixir-classes/ But I don't use Elixir, I just use sqlalchemy directly, and define my models as: Base = declarative_base() class User(Base): __tablename__ = "users" ... def send_emai...

How to get the original value of changed fields?

I'm using sqlalchemy as my orm, and use declarative as Base. Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) My question is, how do I know a user has been modified, and how to get the original values without query database again? user = Sess...

Some problems with MapperExtension of sqlalchemy

There are two classes: User and Question A user may have many questions, and it also contains a question_count to record the the count of questions belong to him. So, when I add a new question, I want update the question_count of the user. At first, I do as: question = Question(title='aaa', content='bbb') Session.add(question) ...

What is the `connection` parameter of MapperExtension in sqlalchemy?

The class MapperExtension has some methods, and before_insert, before_update, ... all have a parameter connection. def before_insert(self, mapper, connection, instance): I've read the documents of MapperExtension, but found nothing about this connection. What is it? And how to use it? ...

How to update the user.question_count when the user posted a question?

I'm using sqlalchemy. The question is simple, but I have a big problem, because I want to use MapperExtension. I have two classes: User and Question A user may have many questions, and it also contains a question_count to record the the count of questions belong to him. So, when I add a new question, I want update the question_coun...

SQLAlchemy context sesitive function for creating a composite value on insert and update

I'm trying to sort out how to add a context sensitive function (e.g. def get_user_full_name()) that will get triggered from the default and onupdate specifications on a column. I'm trying to set a composite field that combines the first and last names into a single full name value to avoid having to constantly concat the two fields in a...

Sql Alchemy > TypeError: 'instancemethod' object does not support item assignment

Here's what I've got: from sqlalchemy import * from sqlalchemy.orm import * from web.models.card import * connectionString = "postgresql://www:www@localhost/prod" databaseEngine = create_engine(connectionString) sessionFactory = sessionmaker(autoflush = True, autocommit = False, bind = databaseEngine) session = sessionFactory() CardsCo...

Bulk insert with sqlAlchemy ORM

Is there any way to get sqlalchemy to do a bulk insert rather than inserting each individual object. i.e., doing: INSERT INTO `foo` (`bar`) VALUES (1), (2), (3) rather than: INSERT INTO `foo` (`bar`) VALUES (1) INSERT INTO `foo` (`bar`) VALUES (2) INSERT INTO `foo` (`bar`) VALUES (3) I've just converted some code to use sqlalchemy...

Sqlalchemy seems to commit changes when it's not supposed to

Consider the following snippet of Python code: from sqlalchemy import * from sqlalchemy.orm import * db = create_engine('postgresql:///database', isolation_level='SERIALIZABLE') Session = scoped_session(sessionmaker(bind=db, autocommit=False)) s = Session() s.add(SomeInstance()) s.flush() raw_input('Did it work? ') It connects to the ...

How to insert the 'answer' model?

The title may be not exactly, but I don't know how to express it. I have 3 class: User, Question, Answer. The simple code is: Session = scoped_session(sessionmaker()) Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) questions = relationship('Question', backref="use...

Is elixir out-dated?

My sqlalchemy is 0.6.3, and elixir is 0.7.1 I created a model class which extends Entity: from elixir import * class User(Entity): pass And save the a user as: user = User() user.save() It reports Session has no attribute 'save' I looked into the code of elixir, found it invokes sqlalchemy.org.session.Session#save(), but ther...

How do I cascade deletes to multiple tables in SqlAlchemy?

I have a table with several dependent tables that I want cascade delete. I'm having problems with it cascading too far. Some code will help explain. class Map(Base): .... #One to many relationship between the Map and Tile. #Each Map is made up of many tiles tiles = relationship('Tile', lazy='joined', backref='map', ...

Update a record in a table in SQLAlchemy and Python

I have some problems when I try to update information in some tables. For example, I have this table: class Channel(rdb.Model): rdb.metadata(metadata) rdb.tablename("channels") id = Column("id", Integer, primary_key=True) title = Column("title", String(100)) hash = Column("hash", String(50)) runtime = Column("ru...

How to set filter to get children in certain time period by eagerload_all() at SqlAlchemy

I have a table posts and it stores 3 types of post, Topic, Reply and Comment. Each one has its parent id. # Single table inheritance class Post(Base): __tablename__ = 'posts' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('posts.id')) discriminator = Column(String(1)) content = Column(U...

Problems to update a detached object

Hello guys, I have this table: class Channel(rdb.Model): rdb.metadata(metadata) rdb.tablename("channels") id = Column("id", Integer, primary_key=True) title = Column("title", String(100)) hash = Column("hash", String(50)) runtime = Column("runtime", Float) items = relationship(MediaItem, secondary="channel...

SQLAlchemy Select statement - SQL Syntax Error

I have created a table (MySQL 5.1) from sqlalchemy import * def get(): db = create_engine('mysql://user:password@localhost/database') db.echo = True metadata = MetaData(db) feeds = Table('feeds', metadata, Column('id', Integer, primary_key=True), Column('title', String(100)), Column...