[sqlalchemy] round robin implementation issue

2012-10-03 Thread Paolo Di Prodi
Hello guys,
I am implementing a round robin database for my project in SQLAlchemy and I 
have some problems with TRIGGERS.
Everything works but for each INSERT OR REPLACE, the trigger is not called 
at all so in this case I would have only 25 rows rather than 50.

Any suggestion to debug it?

Here's my code:


def InitRoundRobin(self):
 
 Creates all the tables necessary one for the network swap 
 events and one for the motion
 
 #print Creating database +self.__file_path
 self.__file_event = Table('network', self.__metadata,
 Column('rrd_key', Integer, Sequence('rrd_key_seq'), 
 primary_key=True),
 Column('name', String, default=NONE),
 Column('location', String, default=NONE),
 Column('type', String,default=NUM),
 Column('direction', String,default=INP), 
 Column('value', String,default=0), 
 Column('time',  TIMESTAMP(), default=now()), 
 UniqueConstraint('name', 'time', name='name_time')
 )
 self.__file_event.create()   
 
 self.__file_rrd = Table('rrdkey', self.__metadata,
 Column('rrd_key', Integer, Sequence('rrd_key_seq'), 
 primary_key=True)
 )
 self.__file_rrd.create()
 operation = self.__file_rrd.insert()
 ## attempt an insert
 result=operation.execute(rrd_key=0);
 
 self.rrd_ins = DDL('''\ 
 DROP TRIGGER IF EXISTS rrd_ins;
 DELIMITER $$
 CREATE TRIGGER  rrd_ins
 BEFORE INSERT ON network
 FOR EACH ROW
 BEGIN
   SET @rrd_key = 0;
   SET @rows = 10;
   IF NEW.rrd_key = 0 THEN
 SELECT rrd_key + 1
   FROM rrdkey
   INTO @rrd_key;
 SET NEW.rrd_key = @rrd_key;
   END IF;
   IF (NEW.rrd_key % @rows) THEN
 SET NEW.rrd_key = NEW.rrd_key % @rows;
   ELSE
 SET NEW.rrd_key = @rows;
   END IF;
   UPDATE network SET rrd_key = NEW.rrd_key;
 END;
 $$
 DELIMITER;''')
 event.listen(self.__file_event, 'after_create', self.rrd_ins)


And the REPLACE code:

def addEntryReplace(self, location,name,value,type):
 #print Adding entry
 #operation = self.__file_event.insert()
 t = text(INSERT OR REPLACE INTO network 
 (location,name,value,type,time) VALUES 
 (:location,:name,:value,:type,:time))
 print t
 #operation.prefix_with(OR REPLACE);
 result=self.__connection.execute(t, location=location, 
 name=name, value=value, type=type, time=now()) 


This is called like this:

def loopTest(self):
self.__connection=connection = self.__db.connect()
for i in range(0, 50):
self.addEntryReplace(CASA,PARAM,i,TYPE)
print Added entry , i

self.__connection.close()

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/1vqI9TpN-IEJ.
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.



[sqlalchemy] SQLite rollback() fails

2012-10-03 Thread Gombas, Gabor
Hi,

I've run into a rather interesting issue, where calling session.rollback() 
dies. The error I get with SQLA 0.7 is:

class 'sqlalchemy.exc.InvalidRequestError': Can't attach instance Foo at 
0x2acf68808710; another instance with key (class 'module.Foo', (342,)) is 
already present in this session.

Traceback:

   sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:672:rollback
   sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:386:rollback
   sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:417:_rollback_impl
   sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:274:_restore_snapshot
   sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:1620:_update_impl
   sqlalchemy-rel_0_7/lib/sqlalchemy/orm/session.py:1648:_attach

0.8 gives a slightly different message, but I think it is essentially the same:

type 'exceptions.AssertionError': A conflicting state is already present in 
the identity map for key (class 'module.Foo', (342,))

Traceback:

sqlalchemy-default/lib/sqlalchemy/orm/session.py:637:rollback
sqlalchemy-default/lib/sqlalchemy/orm/session.py:346:rollback
sqlalchemy-default/lib/sqlalchemy/orm/session.py:377:_rollback_impl
sqlalchemy-default/lib/sqlalchemy/orm/session.py:233:_restore_snapshot
sqlalchemy-default/lib/sqlalchemy/orm/session.py:1578:_update_impl
sqlalchemy-default/lib/sqlalchemy/orm/identity.py:115:add

What I see when turning on query logging:


-  The Foo object with primary key 342 is loaded (using a non-primary 
mapper, if that makes any difference), and then deleted. The session is flushed.

-  A new object is created (this time using the primary mapper), and 
during INSERT, SQLite assigns the same primary key to it as the previously 
deleted object had (I can see that from subsequent SELECT statements 
referencing the new object; it would be nice if SQLA logged the primary key 
after an INSERT at debug level)

-  An application error (unrelated to SQLAlchemy) occurs, which causes 
rollback() to be called

I have a reliable reproducer, but it is the unit test of a large application, 
so there are lots of possibly unrelated things going on. I've spent two days 
coming up with a standalone reproducer, but I've failed, that's why I'm sending 
this mail instead of opening a ticket.

Any ideas?

Gabor

--
NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or 
views contained herein are not intended to be, and do not constitute, advice 
within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and 
Consumer Protection Act. If you have received this communication in error, 
please destroy all electronic and paper copies and notify the sender 
immediately. Mistransmission is not intended to waive confidentiality or 
privilege. Morgan Stanley reserves the right, to the extent permitted under 
applicable law, to monitor electronic communications. This message is subject 
to terms available at the following link: 
http://www.morganstanley.com/disclaimers. If you cannot access these links, 
please notify us by reply message and we will send the contents to you. By 
messaging with Morgan Stanley you consent to the foregoing.

-- 
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.



RE: [sqlalchemy] SQLite rollback() fails

2012-10-03 Thread Gombas, Gabor
I've tested the 0.7  0.8 snapshots, and both work fine now. Thanks!

From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On 
Behalf Of Michael Bayer
Sent: 03 October 2012 17:20
To: sqlalchemy@googlegroups.com
Subject: Re: [sqlalchemy] SQLite rollback() fails


On Oct 3, 2012, at 10:34 AM, Michael Bayer wrote:




Catching both of these is easy enough and a patch is added to #2583: 
http://www.sqlalchemy.org/trac/ticket/2583

The SQLite case you describe isn't exactly like either of these; if the object 
were flushed by the session and received the same PK, that case is already 
handled since the object would be in ._new.  So my only guess is that, that's 
not the trigger of the issue.So at the very least you need to try that 
patch and see if it clears things up.

Assuming that's the fix, I don't know that there's a clean way to patch this 
in, except for patching your install or monkeypatching a new Session class in.  
   I should have it committed in both 0.7/0.8 within 30, unless I hit some 
snags in the tests.


OK those are in tip for default + 0.7, so you can get those fixes now from the 
snapshot releases.
--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to 
sqlalchemy@googlegroups.commailto:sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.commailto:sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

--
NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or 
views contained herein are not intended to be, and do not constitute, advice 
within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and 
Consumer Protection Act. If you have received this communication in error, 
please destroy all electronic and paper copies and notify the sender 
immediately. Mistransmission is not intended to waive confidentiality or 
privilege. Morgan Stanley reserves the right, to the extent permitted under 
applicable law, to monitor electronic communications. This message is subject 
to terms available at the following link: 
http://www.morganstanley.com/disclaimers. If you cannot access these links, 
please notify us by reply message and we will send the contents to you. By 
messaging with Morgan Stanley you consent to the foregoing.

-- 
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.



[sqlalchemy] SQLALCHEMY query.

2012-10-03 Thread Trinath Somanchi
Hi-

I have a sql query which is returning 2 rows. But when is transformed to 
ORM query, its not returning any rows. 

My SQL Statement:

select distinct(inst.hostname) as server_name, 
   fip.address as fixed_ip_address,
   vif.address as fixed_ip_mac_address
from instances inst, instance_metadata mtd, virtual_interfaces vif, 
fixed_ips fip
where  inst.id = mtd.instance_id   and
   mtd.instance_id = vif.instance_id  and
   vif.instance_id = fip.instance_id  and
   inst.project_id = 'e216fcb54dc944a8ab16e4e325299643' and
   mtd.key = 'Server Group' and
   mtd.value = 'DOM1'
group by mtd.key,mtd.value;

SQL
+-+--+--+
| server_name | fixed_ip_address | fixed_ip_mac_address |
+-+--+--+
| serverpoc   | 172.15.1.2   | fa:16:3e:56:47:71|
| serverpoc2  | 172.15.1.3   | fa:16:3e:4f:3c:9b|
+-+--+--+



I have written the ORM query as

result = 
session.query(models.Instance.hostname.distinct(),models.FixedIp.address,models.VirtualInterface.address).\
join((models.InstanceMetadata,
  models.InstanceMetadata.instance_id == 
models.Instance.id http://models.instance.id/)).\
join ((models.FixedIp,
  models.FixedIp.instance_id == 
models.InstanceMetadata.instance_id)).\
join ((models.VirtualInterface,
   models.VirtualInterface.instance_id == 
models.FixedIp.instance_id)).\
filter(and_(models.Instance.project_id == 
search_opts['project_id'])).\
filter(and_(models.InstanceMetadata.key == 
str(search_opts['key']) )).\
filter(and_(models.InstanceMetadata.value == 
str(search_opts['value']))).\
all()

Can any one help me find the fault in the ORM query.

Thanks in advance.

-
Trinath

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/a-nPCWf-HTEJ.
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.



Re: [sqlalchemy] SQLALCHEMY query.

2012-10-03 Thread Simon King
On Wed, Oct 3, 2012 at 5:02 PM, Trinath Somanchi
trinath.soman...@gmail.com wrote:
 Hi-

 I have a sql query which is returning 2 rows. But when is transformed to ORM
 query, its not returning any rows.

 My SQL Statement:

 select distinct(inst.hostname) as server_name,
fip.address as fixed_ip_address,
vif.address as fixed_ip_mac_address
 from instances inst, instance_metadata mtd, virtual_interfaces vif,
 fixed_ips fip
 where  inst.id = mtd.instance_id   and
mtd.instance_id = vif.instance_id  and
vif.instance_id = fip.instance_id  and
inst.project_id = 'e216fcb54dc944a8ab16e4e325299643' and
mtd.key = 'Server Group' and
mtd.value = 'DOM1'
 group by mtd.key,mtd.value;

 SQL
 +-+--+--+
 | server_name | fixed_ip_address | fixed_ip_mac_address |
 +-+--+--+
 | serverpoc   | 172.15.1.2   | fa:16:3e:56:47:71|
 | serverpoc2  | 172.15.1.3   | fa:16:3e:4f:3c:9b|
 +-+--+--+



 I have written the ORM query as

 result =
 session.query(models.Instance.hostname.distinct(),models.FixedIp.address,models.VirtualInterface.address).\
 join((models.InstanceMetadata,
   models.InstanceMetadata.instance_id ==
 models.Instance.id)).\
 join ((models.FixedIp,
   models.FixedIp.instance_id ==
 models.InstanceMetadata.instance_id)).\
 join ((models.VirtualInterface,
models.VirtualInterface.instance_id ==
 models.FixedIp.instance_id)).\
 filter(and_(models.Instance.project_id ==
 search_opts['project_id'])).\
 filter(and_(models.InstanceMetadata.key ==
 str(search_opts['key']) )).\
 filter(and_(models.InstanceMetadata.value ==
 str(search_opts['value']))).\
 all()

 Can any one help me find the fault in the ORM query.

 Thanks in advance.


Have you tried turning on SQL logging (eg. by passing echo=True to
create_engine), and comparing the query with your original? If the
query looks ok, maybe the parameters you are passing aren't exactly
what you think they should be.

(Also, I'm not sure if it makes any difference, but those and_()
calls inside filter() are unnecessary - filtering a query already
implies that you are AND-ing the condition with all the previous
conditions)

Simon

-- 
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.



Re: [sqlalchemy] SQLALCHEMY query.

2012-10-03 Thread Trinath Somanchi
Hi Simon-

I have update my ORM query this way 

result = 
session.query(models.Instance.hostname.distinct(),models.FixedIp.address,models.VirtualInterface.address).\
join((models.InstanceMetadata,
  models.InstanceMetadata.instance_id == 
models.Instance.id)).\
join ((models.FixedIp,
  models.FixedIp.instance_id == 
models.InstanceMetadata.instance_id)).\
join ((models.VirtualInterface,
   models.VirtualInterface.instance_id == 
models.FixedIp.instance_id)).\   
 filter(and_(models.Instance.project_id == search_opts['project_id'],
models.InstanceMetadata.key == 
 search_opts['key'],
models.InstanceMetadata.value == 
search_opts['value'])).\
all()


I have received an Programming error

ProgrammingError: (ProgrammingError) (1064, 'You have an error in your SQL 
syntax; check the manual that corresponds to your MySQL server version for 
the right syntax to use near \') AND instance_metadata.value = 
(\'DOM1\',)\' at line 3') 'SELECT DISTINCT instances.hostname AS anon_1, 
fixed_ips.address AS fixed_ips_address, virtual_interfaces.address AS 
virtual_interfaces_address \nFROM instances INNER JOIN instance_metadata ON 
instance_metadata.instance_id = instances.id INNER JOIN fixed_ips ON 
fixed_ips.instance_id = instance_metadata.instance_id INNER JOIN 
virtual_interfaces ON virtual_interfaces.instance_id = 
fixed_ips.instance_id \nWHERE instances.project_id = %s AND 
instance_metadata.`key` = %s AND instance_metadata.value = %s' 
('e216fcb54dc944a8ab16e4e325299643', ['Server Group'], ['DOM1'])

Can you help me troubleshoot the issue.

Thanks a lot for the reply.

-
Trinath

On Wednesday, 3 October 2012 21:47:23 UTC+5:30, Simon King wrote:

 On Wed, Oct 3, 2012 at 5:02 PM, Trinath Somanchi 
 trinath@gmail.com javascript: wrote: 
  Hi- 
  
  I have a sql query which is returning 2 rows. But when is transformed to 
 ORM 
  query, its not returning any rows. 
  
  My SQL Statement: 
  
  select distinct(inst.hostname) as server_name, 
 fip.address as fixed_ip_address, 
 vif.address as fixed_ip_mac_address 
  from instances inst, instance_metadata mtd, virtual_interfaces vif, 
  fixed_ips fip 
  where  inst.id = mtd.instance_id   and 
 mtd.instance_id = vif.instance_id  and 
 vif.instance_id = fip.instance_id  and 
 inst.project_id = 'e216fcb54dc944a8ab16e4e325299643' and 
 mtd.key = 'Server Group' and 
 mtd.value = 'DOM1' 
  group by mtd.key,mtd.value; 
  
  SQL 
  +-+--+--+ 
  | server_name | fixed_ip_address | fixed_ip_mac_address | 
  +-+--+--+ 
  | serverpoc   | 172.15.1.2   | fa:16:3e:56:47:71| 
  | serverpoc2  | 172.15.1.3   | fa:16:3e:4f:3c:9b| 
  +-+--+--+ 
  
  
  
  I have written the ORM query as 
  
  result = 
  
 session.query(models.Instance.hostname.distinct(),models.FixedIp.address,models.VirtualInterface.address).\
  

  join((models.InstanceMetadata, 
models.InstanceMetadata.instance_id == 
  models.Instance.id)).\ 
  join ((models.FixedIp, 
models.FixedIp.instance_id == 
  models.InstanceMetadata.instance_id)).\ 
  join ((models.VirtualInterface, 
 models.VirtualInterface.instance_id == 
  models.FixedIp.instance_id)).\ 
  filter(and_(models.Instance.project_id == 
  search_opts['project_id'])).\ 
  filter(and_(models.InstanceMetadata.key == 
  str(search_opts['key']) )).\ 
  filter(and_(models.InstanceMetadata.value == 
  str(search_opts['value']))).\ 
  all() 
  
  Can any one help me find the fault in the ORM query. 
  
  Thanks in advance. 
  

 Have you tried turning on SQL logging (eg. by passing echo=True to 
 create_engine), and comparing the query with your original? If the 
 query looks ok, maybe the parameters you are passing aren't exactly 
 what you think they should be. 

 (Also, I'm not sure if it makes any difference, but those and_() 
 calls inside filter() are unnecessary - filtering a query already 
 implies that you are AND-ing the condition with all the previous 
 conditions) 

 Simon 


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/jGkraGmdWzQJ.
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.



Re: [sqlalchemy] SQLALCHEMY query.

2012-10-03 Thread Simon King
On Wed, Oct 3, 2012 at 5:23 PM, Trinath Somanchi
trinath.soman...@gmail.com wrote:
 Hi Simon-

 I have update my ORM query this way

 result =
 session.query(models.Instance.hostname.distinct(),models.FixedIp.address,models.VirtualInterface.address).\
 join((models.InstanceMetadata,
   models.InstanceMetadata.instance_id ==
 models.Instance.id)).\
 join ((models.FixedIp,
   models.FixedIp.instance_id ==
 models.InstanceMetadata.instance_id)).\
 join ((models.VirtualInterface,
models.VirtualInterface.instance_id ==
 models.FixedIp.instance_id)).\
 filter(and_(models.Instance.project_id == search_opts['project_id'],
 models.InstanceMetadata.key ==
 search_opts['key'],
 models.InstanceMetadata.value ==
 search_opts['value'])).\
 all()


 I have received an Programming error

 ProgrammingError: (ProgrammingError) (1064, 'You have an error in your SQL
 syntax; check the manual that corresponds to your MySQL server version for
 the right syntax to use near \') AND instance_metadata.value =
 (\'DOM1\',)\' at line 3') 'SELECT DISTINCT instances.hostname AS anon_1,
 fixed_ips.address AS fixed_ips_address, virtual_interfaces.address AS
 virtual_interfaces_address \nFROM instances INNER JOIN instance_metadata ON
 instance_metadata.instance_id = instances.id INNER JOIN fixed_ips ON
 fixed_ips.instance_id = instance_metadata.instance_id INNER JOIN
 virtual_interfaces ON virtual_interfaces.instance_id = fixed_ips.instance_id
 \nWHERE instances.project_id = %s AND instance_metadata.`key` = %s AND
 instance_metadata.value = %s' ('e216fcb54dc944a8ab16e4e325299643', ['Server
 Group'], ['DOM1'])

 Can you help me troubleshoot the issue.


The parameters you are passing to the query are:

  ('e216fcb54dc944a8ab16e4e325299643', ['Server Group'], ['DOM1'])

but I suspect they should be

  ('e216fcb54dc944a8ab16e4e325299643', 'Server Group', 'DOM1')

ie. the last 2 elements should be strings, not single-element lists.

If you are getting these from a web form, are they perhaps
multiple-selection fields? In which case you'll probably want to
change the search conditions to be something like:

  models.InstanceMetadata.key.in_(search_opts['key'])

However, you should only add that condition if search_opts['key'] is
not empty. (I *think* SA warns you if you pass an empty list to in_)

Simon

-- 
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.



Re: [sqlalchemy] SQLALCHEMY query.

2012-10-03 Thread Trinath Somanchi
Hi-

I have solved the issue mysql.

Just converted the LIST to string for key and value..

and the query is working fine.

Thanks a lot guys for the help..

Thanks a lot Simon...for the precious help./


-
Trinath

On Wednesday, 3 October 2012 21:53:05 UTC+5:30, Trinath Somanchi wrote:

 Hi Simon-

 I have update my ORM query this way 

 result = 
 session.query(models.Instance.hostname.distinct(),models.FixedIp.address,models.VirtualInterface.address).\
 join((models.InstanceMetadata,
   models.InstanceMetadata.instance_id == 
 models.Instance.id)).\
 join ((models.FixedIp,
   models.FixedIp.instance_id == 
 models.InstanceMetadata.instance_id)).\
 join ((models.VirtualInterface,
models.VirtualInterface.instance_id == 
 models.FixedIp.instance_id)).\   
  filter(and_(models.Instance.project_id == search_opts['project_id'],
 models.InstanceMetadata.key == 
  search_opts['key'],
 models.InstanceMetadata.value == 
 search_opts['value'])).\
 all()


 I have received an Programming error

 ProgrammingError: (ProgrammingError) (1064, 'You have an error in your SQL 
 syntax; check the manual that corresponds to your MySQL server version for 
 the right syntax to use near \') AND instance_metadata.value = 
 (\'DOM1\',)\' at line 3') 'SELECT DISTINCT instances.hostname AS anon_1, 
 fixed_ips.address AS fixed_ips_address, virtual_interfaces.address AS 
 virtual_interfaces_address \nFROM instances INNER JOIN instance_metadata ON 
 instance_metadata.instance_id = instances.id INNER JOIN fixed_ips ON 
 fixed_ips.instance_id = instance_metadata.instance_id INNER JOIN 
 virtual_interfaces ON virtual_interfaces.instance_id = 
 fixed_ips.instance_id \nWHERE instances.project_id = %s AND 
 instance_metadata.`key` = %s AND instance_metadata.value = %s' 
 ('e216fcb54dc944a8ab16e4e325299643', ['Server Group'], ['DOM1'])

 Can you help me troubleshoot the issue.

 Thanks a lot for the reply.

 -
 Trinath

 On Wednesday, 3 October 2012 21:47:23 UTC+5:30, Simon King wrote:

 On Wed, Oct 3, 2012 at 5:02 PM, Trinath Somanchi 
 trinath@gmail.com wrote: 
  Hi- 
  
  I have a sql query which is returning 2 rows. But when is transformed 
 to ORM 
  query, its not returning any rows. 
  
  My SQL Statement: 
  
  select distinct(inst.hostname) as server_name, 
 fip.address as fixed_ip_address, 
 vif.address as fixed_ip_mac_address 
  from instances inst, instance_metadata mtd, virtual_interfaces vif, 
  fixed_ips fip 
  where  inst.id = mtd.instance_id   and 
 mtd.instance_id = vif.instance_id  and 
 vif.instance_id = fip.instance_id  and 
 inst.project_id = 'e216fcb54dc944a8ab16e4e325299643' and 
 mtd.key = 'Server Group' and 
 mtd.value = 'DOM1' 
  group by mtd.key,mtd.value; 
  
  SQL 
  +-+--+--+ 
  | server_name | fixed_ip_address | fixed_ip_mac_address | 
  +-+--+--+ 
  | serverpoc   | 172.15.1.2   | fa:16:3e:56:47:71| 
  | serverpoc2  | 172.15.1.3   | fa:16:3e:4f:3c:9b| 
  +-+--+--+ 
  
  
  
  I have written the ORM query as 
  
  result = 
  
 session.query(models.Instance.hostname.distinct(),models.FixedIp.address,models.VirtualInterface.address).\
  

  join((models.InstanceMetadata, 
models.InstanceMetadata.instance_id == 
  models.Instance.id)).\ 
  join ((models.FixedIp, 
models.FixedIp.instance_id == 
  models.InstanceMetadata.instance_id)).\ 
  join ((models.VirtualInterface, 
 models.VirtualInterface.instance_id == 
  models.FixedIp.instance_id)).\ 
  filter(and_(models.Instance.project_id == 
  search_opts['project_id'])).\ 
  filter(and_(models.InstanceMetadata.key == 
  str(search_opts['key']) )).\ 
  filter(and_(models.InstanceMetadata.value == 
  str(search_opts['value']))).\ 
  all() 
  
  Can any one help me find the fault in the ORM query. 
  
  Thanks in advance. 
  

 Have you tried turning on SQL logging (eg. by passing echo=True to 
 create_engine), and comparing the query with your original? If the 
 query looks ok, maybe the parameters you are passing aren't exactly 
 what you think they should be. 

 (Also, I'm not sure if it makes any difference, but those and_() 
 calls inside filter() are unnecessary - filtering a query already 
 implies that you are AND-ing the condition with all the previous 
 conditions) 

 Simon 



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To view this discussion on 

RE: [sqlalchemy] live access to postgis database to use in ExtJS, OpenLayers, etc

2012-10-03 Thread Gery .


thanks a lot for the answer, I didn't expect that, nice from you. I'll take a 
look at that and see what I keep doing, in fact I have set all the backend and 
client part, the only thing I need is to leave the door open to my pg tables, 
so I can (at first) search inside them and plot the features in OL. Through the 
postgis_geojson.php script and extjs I'm trying that because it allows what I 
need, it's just to find a way to pass the two variables I need and overwrite 
the default ext-comp104 of the xtype. But yes, this is beyond the scope of this 
forum. BTW my question (live access to postgis database) was related to what 
the MapFish guys did in the MapFish demo (great project btw, these guys are 
geniuses), they just use an url to do the search function, I know there is 
bunch of things behind (Pylons, etc), but I just wanted to know if it's 
possible to do it easier than that, perhaps with just SqlAlchemy, but I think a 
RESTful framework could work also.

Many thanks also Michael for your email, helpful as always.

Cheers,

Gery


__
Piensa en el medio ambiente - mantenlo en la pantalla. NO lo imprimas si NO es 
necesario.
Think green - keep it on the screen. Do NOT print if it is NOT necessary.
Denken Sie an die Umwelt - bewahren Sie es auf dem Bildschirm. Drucken Sie 
NICHT, wenn es NICHT notwendig ist.


 From: si...@simonking.org.uk
 Date: Wed, 3 Oct 2012 10:18:47 +0100
 Subject: Re: [sqlalchemy] live access to postgis database to use in ExtJS, 
 OpenLayers, etc
 To: sqlalchemy@googlegroups.com
 
 On Tue, Oct 2, 2012 at 7:26 PM, Gery . gameji...@hotmail.com wrote:
 
  thanks for the email, it seems that you know a lot about this stuff, it'd be
  great if you could share what you know so in that way we will learn from
  you, but as expected you won't do it, your Sorry I can't be more help, is
  very clear as always. But hey anyway, I appreciate that you demostrate how
  you really are.
 
 
 I was just trying to demonstrate that what you are trying to do is
 complicated, with a lot of interactions between different
 technologies. You need to break it down into smaller tasks that you
 can test, and get each piece working before starting on the next.
 
 I recommend that you start by taking the example GeoJSON file from
 http://www.geojson.org/geojson-spec.html#examples and saving that on
 your web server. Change your javascript to load that file, and check
 that you can actually see the data from the file in your application.
 If you can't get this working you'll need to talk to someone who knows
 about OpenLayers (I don't know anything about it)
 
 Once you've done that, put a python script in the cgi-bin directory of
 your web server that looks something like this:
 
 --
 #!/usr/bin/env python
 # CGI scripts start by sending the HTTP headers. The only
 # one you absolutely need is the Content-type header.
 # I'm not sure if OpenLayers requires a specific content type
 # so you may need to experiment here.
 print Content-type: application/json
 
 # In HTTP, a blank line separates the headers from the body
 # of the response
 print 
 
 # Anything else that you print forms the body of the response.
 # In this case, we just want to send the example GeoJSON data.
 print 
 include the example GeoJSON file here
 
 --
 
 Visit http://localhost/cgi-bin/yourscript.py in your web browser and
 verify that you see the GeoJSON data. If that doesn't work, you'll
 probably need to contact the Python mailing list.
 
 Update your Javascript to point to that URL. You should still be able
 to see the data within your application.
 
 Once you've got all that working, you can start adding SQLAlchemy into
 the python script. Let us know when you get there and we might be able
 to help.
 
 Simon
 
 -- 
 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.
 
  

-- 
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.



Re: [sqlalchemy] live access to postgis database to use in ExtJS, OpenLayers, etc

2012-10-03 Thread Kent Tenney
It sounds like you are trying to do at least 6 quite complicated
things all at once, without really understanding any of them. This
will not be easy.

Sigh. The story of my life in one sentence.

On Tue, Oct 2, 2012 at 8:43 AM, Simon King si...@simonking.org.uk wrote:
 On Mon, Oct 1, 2012 at 11:38 PM, Gery . gameji...@hotmail.com wrote:

 thanks but I want to use that live access to search at first through
 ExtJS/GeoExtJS/OpenLayers and through them there is only a url available
 (protocol HTTP), I also need to get the data as GeoJSON, so I think
 GeoAlchemy might not be the right solution, I think. If I'm wrong, please
 I'd love some points about it, thanks.


 It sounds like you are trying to do at least 6 quite complicated
 things all at once, without really understanding any of them. This
 will not be easy.

 1. The client side of your application is presumably written in
 Javascript and HTML, using javascript libraries such as ExtJS and
 OpenLayers. You need to fully understand how these work.

 2. The application will then make HTTP requests to a web server. You
 need to understand at least the basics of HTTP.

 3. The web server might be a single python script, or it could be
 something running behind Apache. You need to understand your web
 server.

 4. The server side of your application might be using any of a number
 of libraries to connect to the web server (such as
 Django/Pyramid/Flask/cgi/mod_wsgi etc.). You need to understand
 whatever mechanism your application is using to speak HTTP.

 5. Your application can use SQLAlchemy and GeoAlchemy to retrieve data
 from postgis into Python data structures. You will need to understand
 postgis, SQLAlchemy, GeoAlchemy and Python.

 6. Your application can then convert those Python data structures into
 GeoJSON. You will need to understand GeoJSON.

 The SQLAlchemy mailing list can help you with exactly one part of this
 (step 5). SQLAlchemy (and GeoAlchemy) is perfectly capable of querying
 multiple tables and retrieving results. But how you accept the HTTP
 request, and how you pass the results back, are completely outside the
 scope of this list and I'm afraid you are unlikely to find much help
 here with it.

 Sorry I can't be more help,

 Simon

 --
 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.


-- 
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.