sqlalchemy

Transaction within transaction

Hello, I want to know if open a transaction inside another is safe and encouraged? I have a method: def foo(): session.begin try: stuffs except Exception, e: session.rollback() raise e session.commit() and a method that calls the first one, inside a transaction: def bar(): stuffs t...

Save or update for FK relationship Sqlalchemy

I've googled, but haven't been able to find the answer to this seemingly simple question. I have two relations, a customer and an order. Each order is associated to a single cusomter, and therefore has a FK relationship to the customer table. The customer relation only stores customer names, and I have set a unique constraint on the cus...

Unable to access database from within a method

I keep receiving the error, "TypeError: 'Shard' object is unsubscriptable." #Establish an on-demand connection to the central database def connectCentral(): engine = engine_from_config(config, 'sqlalchemy.central.') central.engine = engine central.Session.configure(bind=engine) #Establish an on-demand connection to a shard ...

What's the proper war to describe an associative object by SQLalchemy the declarative way

Hello. I'm looking for a way to describe an associative object the declarative way. Beyond storing the foreign keys in the association table, I need to store information like the creation date of the association. Today, my model looks like that : # Define the User class class User(Base): __tablename__ = 'users' # Define User ...

How to remember results from querying SQLAlchemy relations (to implement caching)?

Suppose I have a mapped class Article. It has a relation category that does a query each time I access it (article.category would issue a query to get the category or article). How do I proxy the article.category call so that the result is queried from the database, then remembered and then returned? Thanks, Boda Cydo. ...

What is a good way to handle database objects in python classes?

Should I access global db object directly from within the methods of each class? Or from each method, should I instantiate an instance of the db object? One of my database objects changes depending on the id of the info being accessed so it is created through a function connectToDatabase(id). Should I make this a global function, ha...

In SQLAlchemy, is it possible to declare cascade relations while using declarative style?

In SQLAlchemy, we can declare tables and their relations like this: user = Table( 'users', metadata, Column('id', Integer, primary_key=True)) address = Table( 'adresses', metadata, Column('id', Integer, primary_key=True), Column('user_id', Integer, ForeignKey('user.id'))) class User(object): pass class Address(obj...

Database-Independent MAX() Function in SQLAlchemy

I'd like to calculate a MAX() value for a column. What's the proper way to do this in sqlalchemy while preserving database independence? ...

If I'm only planning to use MySQL, and if speed is a priority, is there any convincing reason to use SQLAlchemy?

SQLAlchemy seems really heavyweight if all I use is MySQL. Why are convincing reasons for/against the use of SQLAlchemy in an application that only uses MySQL. ...

How to cache SQLAlchemy results?

Suppose I have a mapped class Article and I have a mapped class Author. Article.author is a relation on Author that returns the author of the Article. How do I make Article.author to cache the value in memcached? The problem is that referring to Article.author does an SQL query each time it is called, but there is really no need to quer...

Integrating SQLAlchemy's ORM with existing classes in pylons.

I have a class in my existing python project, User, that I would like to map to tables. But I'm not sure what the best way to do this is? Does this mean I can remove: class User: pass from my model/__ init __.py? Or should I leave that in there, and have something like: from project.model.user import User class User: pass ...

Is it possible to get sqlalchemy to create a composite primary key with an integer part without making it an IDENTITY type?

I'm using sqlalchemy 6.0. The SQL Server T-SQL dialect seems to want to make any integer that's part of my primary key into an identity. That may be ok if the integer field were the primary key, but mine is a composite and this isn't going to work for me. Is there a way to suppress this behavior? Here's a demonstration of the problem: ...

SQLAlchemy - Problem with an association table and dates in primary join

Hi, I am working on defining my mapping with SQLAlchemy and I am pretty much done except one thing. I have a 'resource' object and an association table 'relation' with several properties and a relationship between 2 resources. What I have been trying to do almost successfully so far, is to provide on the resource object 2 properties: pa...

Sqlalchemy file organization

Does anyone has any insight on organizing sqlalchemy based projects? I have many tables and classes with foreign keys, and relations. What is everyone doing in terms of separating classes, tables, and mappers ? I am relatively new to the framework, so any help would be appreciated. Example: classA.py # table definition and class A defi...

Converting to declarative method and column_property

Following suggestions, I am converting my project to declarative sqlalchemy class definitions. This allows me to define relations as strings ( a=relation('myotherclass'), instead of relation(myotherclass) ), and avoid cross importing issues. My question is how do I define column_properties the same way. This is sort of what I have (no...

Sqlalchemy - Can we use date comparison in relation definition ?

Hi, I have this mapper defined: mapper(Resource, resource_table, properties = {'type' : relation(ResourceType,lazy = False), 'groups' : relation(Group, secondary = model.tables['resource_group'], backref = 'resources'), 'parent' : relation(Relation, uselist=False, primaryjoin = and_(relation_table.c.res_id == resourc...

Is there a way to transparently perform validation on SQLAlchemy objects?

Is there a way to perform validation on an object after (or as) the properties are set but before the session is committed? For instance, I have a domain model Device that has a mac property. I would like to ensure that the mac property contains a valid and sanitized mac value before it is added to or updated in the database. It looks ...

Elixir create_all() not creating database and tables

Hey, I'm using Elixir 0.7.1 , Sqlalchemy 0.6beta1 , MySQLdb 1.2.2. My Model file 'model.py' looks like this: from elixir import * from datetime import datetime class Author: first_name = Field(Unicode(64)) last_name = Field(Unicode(64)) class Article: title = Field(Unicode(64)) class Category: name = Field(Unicode(64)) setu...

func.max get unexpected result?

I've got a table (story_id, votes) with data [(1,3), (2,4)] when I try to do a query... session.query(table.c.story_id, func.max(table.c.votes)).first() i'll get: (1,4) the result I expect is: (2,4) where is the misunderstanding? ...

Using PyQt4 - QTableView with SQLAlchemy using QSqlTableModel (or not)

Hi everyone, i'm starting to learn Qt for python and as i was wondering after reading this post : qt - pyqt QTableView not populating when changing databases. if there was a way to use SQLAlchemy sessions instead of (re-)opening a database connection as a Table Model with Qt's QTableView widget. Something that would work a little bit l...