[sqlalchemy] query with use of index

2015-04-23 Thread Victor Poluksht


create table posts (
id serial primary key,
view_count integer default 0 not null,
vote_count integer default 0 not null
);
 
create index on posts((case view_count when 0 then 0 else vote_count::float / 
view_count end));
 
class Post(SQL_DB.meta):
__table__ = sa.Table('posts', SQL_DB.meta.metadata, autoload=True)
 
I need to query Post, ordered by vote_count/view_count with use of index.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] session.add order

2015-02-15 Thread Victor Poluksht
I've found that sometimes sqlalchemy inserts objects during 
session.commit() not in the order they have been added using session.add()

I've put my code example and the output to the github gist.

https://gist.github.com/vpol/8da4a512308ae351eaf6

My question is similar to this one: 
http://stackoverflow.com/questions/10154343/is-sqlalchemy-saves-order-in-adding-objects-to-session

Is it possible to make sqlalchemy guarantee the order of insert.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Writing a query

2011-02-14 Thread Victor Poluksht
I have two classes

table_external_numbers = sa.Table('external_numbers', meta.metadata,
   sa.Column('en_id', sa.types.Integer, primary_key = True),
   sa.Column('en_prefix', sa.types.Integer, nullable = False),
   sa.Column('en_use_called', sa.types.Boolean, nullable = False),
)

class ExternalNumbers(object): pass
orm.mapper(ExternalNumbers, table_external_numbers)

and 

table_gateways = sa.Table('gateways', meta.metadata,
   sa.Column('gw_id', sa.types.Integer, primary_key = True),
   sa.Column('gw_active', sa.types.Boolean, nullable = False),
   sa.Column('gw_server', sa.types.Unicode(64), nullable = True),
   sa.Column('gw_maxlines', sa.types.Integer, nullable = False),
   sa.Column('gw_curlines', sa.types.Integer, nullable = False)
)

class Gateways(object): pass
orm.mapper(Gateways, table_gateways, properties = {'external_numbers': 
orm.relation(ExternalNumbers, secondary = table_gateways_external_numbers, 
backref = 'gateways')})

and also many-to-many relation table 
table_gateways_external_numbers = sa.Table('gateways_external_numbers', 
meta.metadata,
   sa.Column('gw', sa.types.Integer, sa.ForeignKey('gateways.gw_id'), 
nullable = False),
   sa.Column('en', sa.types.Integer, 
sa.ForeignKey('external_numbers.en_id'), nullable = False)
)

I need to select all records from both tables (gateways, external_numbers) 
joined but only that one, that have relation.

The raw query looks like:

select * from external_numbers e, gateways g, gateways_external_numbers ge 
WHERE
ge.en = e.en_id AND
ge.gw = g.gw_id AND
(e.en_prefix = 100 and g.gw_active is TRUE and g.gw_server = 
'XXX.XXX.XXX.XXX') OR
(e.en_prefix = 101 and g.gw_active is TRUE and g.gw_server = 
'YYY.YYY.YYY.YYY')

I have trouble writing a sqlalchemy query doing the same.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.