Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-14 Thread Richard Vézina
form.record.delete() not working, instead use form.record.delete_record()

Richard

On Wed, Dec 12, 2012 at 1:11 PM, Richard Vézina ml.richard.vez...@gmail.com
 wrote:

 Thanks Massimo...

 deletable = True was missing :

 def create_update():
 create update funciton
 form = SQLFORM(db[request.args(0)], request.args(1), deletable = True)
 form.process(dbio=False)
 if form.accepted:
 if form.deleted: # to be deleted because dbio=False
 count =  db(db.table3.field2t3 == form.record_id).count()
 if count:
 session.flash = T('The record you try to delete is still
 referenced by other records and can\'t be deleted')
 redirect(URL(c='default', f='create_update',
 args=request.args(0)))
 else:
 form.record.delete()
 else:
 form.record.update_record(**form.post_vars)
 return dict(form=form)

 Richard

 On Wed, Dec 12, 2012 at 12:48 PM, Massimo Di Pierro 
 massimo.dipie...@gmail.com wrote:

 form = SQLFORM(db.table, record_id)
 form.process(dbio=False)
 if form.accepted:
 if form.deleted: # to be deleted because dbio=False
 count =  db(db.table3.field2t3 == form.record_id).count()
 if count:
   session.flash = T('The record you try to delete is still
 referenced by other records and can\'t be deleted')
   redirect(URL(c='default', f='create_update',
 args=request.args(0)))
 else:
   form.record.delete()
 else:
 form.record.update_record(**form.post_vars)




-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-12 Thread Richard Vézina
I am searching a way to avoid this kind of ticket in web2py :

class 'psycopg2.IntegrityError' ERREUR: UPDATE ou DELETE sur la table «
table2 » viole la contrainte de clé étrangère « table3_field2t3_fkey » de
la table « table3 » DETAIL: La clé (id)=(1) est toujours référencée à
partir de la table « table3 ».

See this thread :

https://mail.google.com/mail/?shva=1#search/ondelete/13b86b9d988ec725

Thanks

Richard

On Tue, Dec 11, 2012 at 6:33 PM, Massimo Di Pierro 
massimo.dipie...@gmail.com wrote:

 Can you explain us what is the purpose of this code?

-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-12 Thread Richard Vézina
Not sure the URL will work :

https://groups.google.com/forum/?fromgroups=#!topic/web2py/ZSRBsudl7dc

Richard

On Wed, Dec 12, 2012 at 9:37 AM, Richard Vézina ml.richard.vez...@gmail.com
 wrote:

 I am searching a way to avoid this kind of ticket in web2py :

 class 'psycopg2.IntegrityError' ERREUR: UPDATE ou DELETE sur la table «
 table2 » viole la contrainte de clé étrangère « table3_field2t3_fkey » de
 la table « table3 » DETAIL: La clé (id)=(1) est toujours référencée à
 partir de la table « table3 ».

 See this thread :

 https://mail.google.com/mail/?shva=1#search/ondelete/13b86b9d988ec725

 Thanks

 Richard


 On Tue, Dec 11, 2012 at 6:33 PM, Massimo Di Pierro 
 massimo.dipie...@gmail.com wrote:

 Can you explain us what is the purpose of this code?




-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-12 Thread Massimo Di Pierro
Sorry I had missed the context. Now I understand. The point is, you cannot 
catch this exception and expect things will work. The DB will enforce the 
reference. Catching the web2py exception does not mean the db will do it.

You have two options:

1) 

ondelete = SET NULL

This should work but you cannot just add it because web2py does not do 
migration when ondelete changes. You have to delete your database, you 
.tables and start again.

2) set to null manually:

def ondelete_func(form):
for field in form.table._referenced_by:
db(field==form.record_id).update(**{field.name:None})

def create_update():
create update funciton
#crud.settings.update_ondelete = StorageList()
form = crud.update(db[request.args(0)], request.args(1), 
ondelete=ondelete_func)
return dict(form=form)


On Wednesday, 12 December 2012 08:37:46 UTC-6, Richard wrote:

 I am searching a way to avoid this kind of ticket in web2py :

 class 'psycopg2.IntegrityError' ERREUR: UPDATE ou DELETE sur la table « 
 table2 » viole la contrainte de clé étrangère « table3_field2t3_fkey » de 
 la table « table3 » DETAIL: La clé (id)=(1) est toujours référencée à 
 partir de la table « table3 ». 

 See this thread :

 https://mail.google.com/mail/?shva=1#search/ondelete/13b86b9d988ec725

 Thanks

 Richard

 On Tue, Dec 11, 2012 at 6:33 PM, Massimo Di Pierro 
 massimo@gmail.comjavascript:
  wrote:

 Can you explain us what is the purpose of this code?




-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-12 Thread Richard Vézina
Ok, I understand, but I don't want the record to be deleted, so SET TO NULL
not a solution.

Now I try what I was trying to avoid :

def ondelete_func2(form):
count = 0
count += db(db.table3.field2t3 == request.args(1)).count()
if count  0:
pass
else:
session.flash = T('The record you try to delete is still referenced
by other records and can\'t be deleted')
redirect(URL(c='default', f='create_update', args=request.args(0)))

I want to look into table3 if a id of table2 is still referenced.

I know that I don't need count+= it is like that because in my own app
there is not only one table3 there is 30+ tables that are referencing id of
table2 and that why I would avoid using query to be informed about that.

Now what I don't know how to do is to prevent the crud.update() deletion to
occur in case of count  0...

Help is appreciate.

Richard

On Wed, Dec 12, 2012 at 12:29 PM, Massimo Di Pierro 
massimo.dipie...@gmail.com wrote:

 add it because web2py does not do migration when ondelete changes. You
 have to delete your database, you .tables and start again.

-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-12 Thread Massimo Di Pierro
Do not sure crud. It is old, to be deprected, less flexible than SQLFORM. 
Do this:

form = SQLFORM(db.table, record_id)
form.process(dbio=False)
if form.accepted:
if form.deleted: # to be deleted because dbio=False
count =  db(db.table3.field2t3 == form.record_id).count()
if count: 
  session.flash = T('The record you try to delete is still 
referenced by other records and can\'t be deleted')
  redirect(URL(c='default', f='create_update', 
args=request.args(0)))
else:
  form.record.delete()
else:
form.record.update_record(**form.post_vars)

On Wednesday, 12 December 2012 11:37:26 UTC-6, Richard wrote:

 Ok, I understand, but I don't want the record to be deleted, so SET TO 
 NULL not a solution.

 Now I try what I was trying to avoid :

 def ondelete_func2(form):
 count = 0
 count += db(db.table3.field2t3 == request.args(1)).count()
 if count  0:
 pass
 else:
 session.flash = T('The record you try to delete is still 
 referenced by other records and can\'t be deleted')
 redirect(URL(c='default', f='create_update', args=request.args(0)))

 I want to look into table3 if a id of table2 is still referenced.

 I know that I don't need count+= it is like that because in my own app 
 there is not only one table3 there is 30+ tables that are referencing id of 
 table2 and that why I would avoid using query to be informed about that.

 Now what I don't know how to do is to prevent the crud.update() deletion 
 to occur in case of count  0...

 Help is appreciate.

 Richard

 On Wed, Dec 12, 2012 at 12:29 PM, Massimo Di Pierro 
 massimo@gmail.comjavascript:
  wrote:

 add it because web2py does not do migration when ondelete changes. You 
 have to delete your database, you .tables and start again.




-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-12 Thread Richard Vézina
Thanks Massimo...

deletable = True was missing :

def create_update():
create update funciton
form = SQLFORM(db[request.args(0)], request.args(1), deletable = True)
form.process(dbio=False)
if form.accepted:
if form.deleted: # to be deleted because dbio=False
count =  db(db.table3.field2t3 == form.record_id).count()
if count:
session.flash = T('The record you try to delete is still
referenced by other records and can\'t be deleted')
redirect(URL(c='default', f='create_update',
args=request.args(0)))
else:
form.record.delete()
else:
form.record.update_record(**form.post_vars)
return dict(form=form)

Richard

On Wed, Dec 12, 2012 at 12:48 PM, Massimo Di Pierro 
massimo.dipie...@gmail.com wrote:

 form = SQLFORM(db.table, record_id)
 form.process(dbio=False)
 if form.accepted:
 if form.deleted: # to be deleted because dbio=False
 count =  db(db.table3.field2t3 == form.record_id).count()
 if count:
   session.flash = T('The record you try to delete is still
 referenced by other records and can\'t be deleted')
   redirect(URL(c='default', f='create_update',
 args=request.args(0)))
 else:
   form.record.delete()
 else:
 form.record.update_record(**form.post_vars)


-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Derek
IntegrityError is a subclass of Database - you need to catch 
psycopg2.database.integrityerror

On Tuesday, December 11, 2012 9:43:04 AM UTC-7, Richard wrote:

 Hello,

 I try to write a ondelete function that will try to delete a record and if 
 fall on the exception : class 'psycopg2.IntegrityError'

 Will trigger a flash message, here the code :

 def ondelete_func(form):
 try delete ondelete if database raise an error trigger a message 
 telling the user that the record can't be deleted
 try:
 crud.delete(db[request.args(0)], request.args(1))
 except IntegrityError:
 session.flash = T('The record you try to delete is still 
 referenced by other records and can\'t be deleted')
 
 def create_update():
 create update funciton
 #crud.settings.update_ondelete = StorageList()
 form = crud.update(db[request.args(0)], request.args(1), 
 ondelete=ondelete_func)
 return dict(form=form)

 I also try with : except psycopg2.IntegrityError:

 No chance.

 Thanks

 Richard


-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Richard Vézina
type 'exceptions.NameError' global name 'psycopg2' is not defined

Richard

On Tue, Dec 11, 2012 at 11:57 AM, Derek sp1d...@gmail.com wrote:

 psycopg2.database.integrityerror


-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Niphlod

to catch the exceptions from a module, you should import it

import psycopg2



in your controller will let you do:

try:
db(db.table2.id0).delete()
except psycopg2.IntegrityError:
session.flash = can't do this and that

However, instead of resorting to catch the integrityerror that in the 
future may change, I'd go with the route that sees if in the child table(s) 
there are records attached.


-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Richard Vézina
I have to see if record of table2 is attached by table3 in my example, but
in my app actually, table3 is 30 differents tables that each can refer to
table2 entry multiple time (one to many)...

I would prefer to do what you suggest child tables but I am afraid of
overhead.

Richard

On Tue, Dec 11, 2012 at 3:32 PM, Niphlod niph...@gmail.com wrote:

 ere are records attached.

-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Massimo Di Pierro
Mand that you if catch a DB exception you must revert or you cannot do 
anything else with the DB within the transaction and you end up with a 
ticket.

On Tuesday, 11 December 2012 14:52:08 UTC-6, Richard wrote:

 I have to see if record of table2 is attached by table3 in my example, but 
 in my app actually, table3 is 30 differents tables that each can refer to 
 table2 entry multiple time (one to many)...

 I would prefer to do what you suggest child tables but I am afraid of 
 overhead.

 Richard

 On Tue, Dec 11, 2012 at 3:32 PM, Niphlod nip...@gmail.com 
 javascript:wrote:

 ere are records attached.




-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Richard Vézina
By the way import psycopg2

Doesn't seem to help.

Richard

On Tue, Dec 11, 2012 at 3:52 PM, Richard Vézina ml.richard.vez...@gmail.com
 wrote:

 I have to see if record of table2 is attached by table3 in my example, but
 in my app actually, table3 is 30 differents tables that each can refer to
 table2 entry multiple time (one to many)...

 I would prefer to do what you suggest child tables but I am afraid of
 overhead.

 Richard

 On Tue, Dec 11, 2012 at 3:32 PM, Niphlod niph...@gmail.com wrote:

 ere are records attached.




-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Richard Vézina
So, it may be the reason I get internal error then. And the Niphold code is
working. I make test.

Richard

On Tue, Dec 11, 2012 at 4:40 PM, Massimo Di Pierro 
massimo.dipie...@gmail.com wrote:

 Mand that you if catch a DB exception you must revert or you cannot do
 anything else with the DB within the transaction and you end up with a
 ticket.


 On Tuesday, 11 December 2012 14:52:08 UTC-6, Richard wrote:

 I have to see if record of table2 is attached by table3 in my example,
 but in my app actually, table3 is 30 differents tables that each can refer
 to table2 entry multiple time (one to many)...

 I would prefer to do what you suggest child tables but I am afraid of
 overhead.

 Richard


 On Tue, Dec 11, 2012 at 3:32 PM, Niphlod nip...@gmail.com wrote:

 ere are records attached.


  --





-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Richard Vézina
Do I am doing right?

def ondelete_func(form):
try delete ondelete if database raise an error trigger a message
telling the user that the record can't be deleted
import psycopg2
try:
db(db[request.args(0)].id==request.args(1)).delete()
except psycopg2.IntegrityError:
db.rollback()
session.flash = T('The record you try to delete is still referenced
by other records and can\'t be deleted')

def create_update():
create update funciton
form = crud.update(db[request.args(0)], request.args(1),
onvalidation=ondelete_func)
return dict(form=form)

I still get this ticket :

class 'psycopg2.IntegrityError' ERREUR: UPDATE ou DELETE sur la table «
table2 » viole la contrainte de clé étrangère « fk_table3 » de la table «
table3 » DETAIL: La clé (id)=(2) est toujours référencée à partir de la
table « table3 »


And I am not sure how I can revert the crud.update deletion the way I code
that.

Thanks

Richard

On Tue, Dec 11, 2012 at 4:45 PM, Richard Vézina ml.richard.vez...@gmail.com
 wrote:

 So, it may be the reason I get internal error then. And the Niphold code
 is working. I make test.

 Richard


 On Tue, Dec 11, 2012 at 4:40 PM, Massimo Di Pierro 
 massimo.dipie...@gmail.com wrote:

 Mand that you if catch a DB exception you must revert or you cannot do
 anything else with the DB within the transaction and you end up with a
 ticket.


 On Tuesday, 11 December 2012 14:52:08 UTC-6, Richard wrote:

 I have to see if record of table2 is attached by table3 in my example,
 but in my app actually, table3 is 30 differents tables that each can refer
 to table2 entry multiple time (one to many)...

 I would prefer to do what you suggest child tables but I am afraid of
 overhead.

 Richard


 On Tue, Dec 11, 2012 at 3:32 PM, Niphlod nip...@gmail.com wrote:

 ere are records attached.


  --







-- 





Re: [web2py] How may I catch this exception class 'psycopg2.IntegrityError'

2012-12-11 Thread Massimo Di Pierro
There are some problem with this although they are not the case of your 
traceback.

- One problem is hat this allows any visitor to delete any record in any 
table. But you know that.
- The second problem is that you deleted the record you are editing on 
validation, before it gets updated. When web2py calls update_record the 
record has been deleted. I do not think the exception happens where you 
catch it. It happens later when update_record is called.

Can you explain us what is the purpose of this code?

On Tuesday, 11 December 2012 15:50:25 UTC-6, Richard wrote:

 Do I am doing right?

 def ondelete_func(form):
 try delete ondelete if database raise an error trigger a message 
 telling the user that the record can't be deleted
 import psycopg2
 try:
 db(db[request.args(0)].id==request.args(1)).delete()
 except psycopg2.IntegrityError:
 db.rollback()
 session.flash = T('The record you try to delete is still 
 referenced by other records and can\'t be deleted')

 def create_update():
 create update funciton
 form = crud.update(db[request.args(0)], request.args(1), 
 onvalidation=ondelete_func)
 return dict(form=form)

 I still get this ticket :

 class 'psycopg2.IntegrityError' ERREUR: UPDATE ou DELETE sur la table « 
 table2 » viole la contrainte de clé étrangère « fk_table3 » de la table « 
 table3 » DETAIL: La clé (id)=(2) est toujours référencée à partir de la 
 table « table3 » 


 And I am not sure how I can revert the crud.update deletion the way I code 
 that.

 Thanks

 Richard

 On Tue, Dec 11, 2012 at 4:45 PM, Richard Vézina 
 ml.richa...@gmail.comjavascript:
  wrote:

 So, it may be the reason I get internal error then. And the Niphold code 
 is working. I make test.

 Richard


 On Tue, Dec 11, 2012 at 4:40 PM, Massimo Di Pierro 
 massimo@gmail.comjavascript:
  wrote:

 Mand that you if catch a DB exception you must revert or you cannot do 
 anything else with the DB within the transaction and you end up with a 
 ticket.


 On Tuesday, 11 December 2012 14:52:08 UTC-6, Richard wrote:

 I have to see if record of table2 is attached by table3 in my example, 
 but in my app actually, table3 is 30 differents tables that each can refer 
 to table2 entry multiple time (one to many)...

 I would prefer to do what you suggest child tables but I am afraid of 
 overhead.

 Richard


 On Tue, Dec 11, 2012 at 3:32 PM, Niphlod nip...@gmail.com wrote:

 ere are records attached.


  -- 
  
  
  





--