How to insert rows in a database from a html page in web2py?

100 views
Skip to first unread message

Rick

unread,
Jul 6, 2023, 7:47:50 AM7/6/23
to web2py-users

How to insert rows from a view-file? The site doesn't produce any error, but an empty row is added to the database.:

From the model file

db.define_table('next_list', Field('node'), Field('next_node'))


From the view file:

{{nextrows = db(db.next_list)}}
{{db.next_list.insert(**db.next_list._filter_fields({ "db.next_list.node" : "test1" , "db.next_list.next_node":"test2" }))}}



What's wrong? Thanks in advance for help and ideas!



Massimiliano

unread,
Jul 6, 2023, 9:02:05 AM7/6/23
to web...@googlegroups.com
try this:

{{db.next_list.insert(**db.next_list._filter_fields({ "node" : "test1" , "next_node":"test2" }))}}

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/ac174d5a-8245-44e5-b708-08258a60f70fn%40googlegroups.com.


--
Massimiliano

Rick

unread,
Jul 6, 2023, 10:07:33 AM7/6/23
to web2py-users
That unfortunately gives a ticket with this information:
sqlite3.IntegrityError: FOREIGN KEY constraint failed

Massimiliano

unread,
Jul 6, 2023, 10:12:59 AM7/6/23
to web...@googlegroups.com
Maybe on your model there is more than you showed :

db.define_table('next_list', Field('node'), Field('next_node'))
db.next_list.insert(**db.next_list._filter_fields({ "node" : "test1" , "next_node":"test2" }))
print(db(db.next_list).select())

next_list.id,next_list.node,next_list.next_node
1,test1,test2

db.next_list.insert(**db.next_list._filter_fields({ "node" : "test1" , "next_node":"test2" }))
print(db(db.next_list).select())

next_list.id,next_list.node,next_list.next_node
1,test1,test2
2,test1,test2




--
Massimiliano

Rick

unread,
Jul 7, 2023, 10:16:06 AM7/7/23
to web2py-users
Thanks Massimiliano for the answering quickly. The suggested code produces this error:
IntegrityError(FOREIGN KEY constraint failed)

If I change the model file lines to 
db.define_table('next_list', Field('node'), Field('next_node', text))
then the error changes to:
NameError(name 'text' is not defined)

Rick

unread,
Jul 8, 2023, 4:53:51 AM7/8/23
to web2py-users
Thank you for pointing out that there was an issue with the database. After deleting the content of the database folder everything works. Tanks for the help!
Reply all
Reply to author
Forward
0 new messages