sqlalchemy

How to arrange the source code of an application made with SQLAlchemy and a graphic interface?

I'm developing an application using SQLAlchemy and wxPython that I'm trying to keep distributed in separated modules consisting of Business logic, ORM and GUI. I'm not completely sure how to do this in a pythonic way. Given that mapping() has to be called in orther for the objects to be used, I thought of putting it on the __init__.py ...

Compound UniqueConstraint with a function

Hi everyone! A quick SQLAlchemy question... I have a class "Document" with attributes "Number" and "Date". I need to ensure that there's no duplicated number for the same year, is there a way to have a UniqueConstraint on "Number + year(Date)"? Should I use a unique Index instead? How would I declare the functional part? (SQLAlchemy...

Python SQLAlchemy/Elixer Question

I am trying to define a SQLAlchemy/Elixer model that can describe the following relationship. I have an SSP table, which has multiple Foreign Keys to the POC table. I've defined the ManyToOne relationships correctly within the SSP object (allowing me to SSP.get(1).action.first_name correctly). What I would also like to add is the other s...

Random ids in sqlalchemy (pylons)

I'm using pylons and sqlalchemy and I was wondering how I could have some randoms ids as primary_key. ...

Attribute as dict of lists using middleman table with SQLAlchemy

My question is about SQLAlchemy but I'm having troubles explaining it in words so I figured I explain it with a simple example of what I'm trying to achieve: parent = Table('parent', metadata, Column('parent_id', Integer, primary_key=True), Column('name', Unicode), ) parent_child = Table('parent_child', metadata, Column('par...

Inserting two related objects fail in SQLAlchemy

Hi Community, I'm getting the (probably trivial) error, but completely clueless about the possible causes. I want to insert two object in the DB using SQLAlchemy. Those objects are related, here are the declarations. Class User: class User(Base): __tablename__ = 'cp_user' id = Column(Integer, Sequence('id_seq'), primary_key=Tr...

Must two SQLAlchemy declarative models share the same declarative_base()?

Is it necessary for two SQLAlchemy models to inherit from the same instance of declarative_base() if they must participate in the same Session? This is likely to be the case when importing two or more modules that define SQLAlchemy models. from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class SomeClas...

SQLAlchemy: Shallow copy avoiding lazy loading

Hi everyone! I'm trying to automatically build a shallow copy of a SA-mapped object.. At the moment my function is just: newobj = src.__class__() for prop in class_mapper(src.__class__).iterate_properties: setattr(newobj, prop.key, getattr(src, prop.key)) but I'm having troubles with lazy relations... Obviously getattr triggers th...

Is there a convenient way to alias only conflicting columns when joining tables in SQLAlchemy?

Sometimes it is useful to map a class against a join instead of a single table when using SQLAlchemy's declarative extension. When column names collide, usually in a one-to-many because all primary keys are named id by default, you can use .alias() to prefix every column with its table name. That is inconvenient if you've already written...

SQLAlchemy declarative concrete autoloaded table inheritance

I've an already existing database and want to access it using SQLAlchemy. Because, the database structure's managed by another piece of code (Django ORM, actually) and I don't want to repeat myself, describing every table structure, I'm using autoload introspection. I'm stuck with a simple concrete table inheritance. Payment ...

SQLAlchemy: relation in mappers compile result of function rather than calling the function when the relation is queried

I have a number of mappers that look like this: mapper(Photo,photo_table, properties = { "locale": relation(PhotoContent, uselist=False, primaryjoin=and_(photo_content_table.c.photoId == photo_table.c.id, photo_content_table.c.locale == get_lang()), foreign_keys=[photo_content_table.c.photoId, photo_content_table.c.locale]) I have dep...

How to check for pending operations in a PostgreSQL transaction

I have a session (SQLAlchemy) on PostgreSQL, with an active uncommitted transaction. I have just passed the session to some call tree that may or may not have issued SQL INSERT/UPDATE/DELETE statements, through sqlalchemy.orm or directly through the underlying connection. Is there a way to check whether there are any pending data-modify...

Nested transactions with SQLAlchemy and sqlite

I'm writing an application in python using sqlalchemy (and Elixir) with sqlite as the database backend. I start a new transaction using the code session.begin_transaction(), but when I call session.rollback() I get the following error: sqlalchemy.exceptions.OperationalError: (OperationalError) no such savepoint: sa_savepoint_1 u'ROLLBAC...

sqlalchemy: AttributeError: 'tuple' object has no attribute 'insert'

I was playing around making a simple haiku site using sqlalchemy and pylons. It basically takes a haiku, writes it to a database, and displays the haiku. The problem appears when I get the data from the form and try and write it to a database, Pylons give me this error: AttributeError: 'tuple' object has no attribute 'insert' after I run...

How do I write an external dialect for SQLAlchemy?

I wrote a minimal database dialect for SQLAlchemy that doesn't really belong in the core. How do I make it work as its own Python package? ...

How do I get the value of a property corresponding to a SQLAlchemy InstrumentedAttribute?

Given a SQLAlchemy mapped class Table and an instance of that class t, how do I get the value of t.colname corresponding to the sqlalchemy.org.attributes.InstrumentedAttribute instance Table.colname? What if I need to ask the same question with a Column instead of an InstrumentedAttribute? Given a list of columns in an ORDER BY clause ...

Alternate forms libraries on django eg sprox, formalchemy

Has anyone had any joy/pain with using other form libraries in django projects (with SQLAlchemy models initially, but perhaps to use with django models in future)? Initial impressions are that sprox is more flexible and decoupled but that formalchemy might be quicker to get up and running with, but I'd be really interested in hearing fr...

Are there database testing tools for python (like sqlunit)?

Are there database testing tools for python (like sqlunit)? I want to test the DAL that is built using sqlalchemy ...

SQLAlchemy 0.5.5 - defining table schema

I used sqlautocode to generate a model.py for an existing MySQL database and here's a table example: fusion_articles = Table('fusion_articles', metadata, Column(u'article_id', Integer(), primary_key=True, nullable=False), Column(u'article_cat', Integer(), primary_key=False, nullable=False), Column(u'article_...

Elixir not creating my tables with default values

class MyObject(Entity): name = Field(Unicode(256), default=u'default name', nullable=False) using_options(shortnames=True) using_mapper_options(save_on_init=False) def __init__(self): self.name = None I am using MySQL in this case, but have also checked against SQLite and I get the same result. It respects nul...