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()...
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)
...
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...
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...
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...
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...
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)
...
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?
...
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...
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...
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...
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...
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 ...
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...
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...
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',
...
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...
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...
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...
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...