FOREIGN KEY constraint failed

679 views
Skip to first unread message

mostwanted

unread,
Jul 2, 2017, 4:37:22 AM7/2/17
to web2py-users
Hi guys, i need help, i'm pulling my hairs out over here, i have a website where when i'm trying to record an entry i get an error:

<class 'sqlite3.IntegrityError'> FOREIGN KEY constraint failed


I have been able to create 2 forms but other attempts to create more forms fail!!


These are my tables;


db.define_table('government',
                Field('govName', requires=IS_NOT_EMPTY()),
                Field('tel', requires=IS_NOT_EMPTY()),
                Field('email', requires=IS_NOT_EMPTY()),
                format='%(govName)s',  migrate=False,fake_migrate=True)


db.define_table('governmenttForm',
                Field('formImage', 'upload'),
                Field('filledformImage', 'upload'),
                Field('formName',requires=IS_NOT_EMPTY()),
                Field('governmentName', 'reference government'),
                format='%(formName)s',migrate=False,fake_migrate=True)

db.define_table('governmetFormPages',
                Field('formImage', 'upload'),
                Field('formTitle',requires=IS_NOT_EMPTY()),
                Field('formName', 'reference govmntForm'),migrate=False,fake_migrate=True)


I am able to create a form for the governmentForm table but when i try to load up the pages for governmentFormPages table i get the error

<class 'sqlite3.IntegrityError'> FOREIGN KEY constraint failed


Below is the stack

Error ticket for "formulator"

Ticket ID

127.0.0.1.2017-07-02.10-06-22.18c363e3-13c2-47f7-ace4-3692239493c4

<class 'sqlite3.IntegrityError'> FOREIGN KEY constraint failed

Version

web2py™ Version 2.14.6-stable+timestamp.2016.05.10.00.21.47

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
Traceback (most recent call last):
File "/home/mostwanted/web2py/gluon/restricted.py", line 227, in restricted
exec ccode in environment
File "/home/mostwanted/web2py/applications/formulator/controllers/appadmin.py", line 703, in <module>
File "/home/mostwanted/web2py/gluon/globals.py", line 417, in <lambda>
self._caller = lambda f: f()
File "/home/mostwanted/web2py/applications/formulator/controllers/appadmin.py", line 151, in insert
if form.accepts(request.vars, session):
File "/home/mostwanted/web2py/gluon/sqlhtml.py", line 1746, in accepts
self.vars.id = self.table.insert(**fields)
File "/home/mostwanted/web2py/gluon/packages/dal/pydal/objects.py", line 726, in insert
ret = self._db._adapter.insert(self, self._listify(fields))
File "/home/mostwanted/web2py/gluon/packages/dal/pydal/adapters/base.py", line 746, in insert
raise e
IntegrityError: FOREIGN KEY constraint failed

Please help me, i dont know what to do, i have tried to look for solutions online but i just dont understand them, may someone please give me light on what i'm doing wrong.

Anthony

unread,
Jul 2, 2017, 1:48:43 PM7/2/17
to web2py-users
Not sure if you're showing your actual code, but the table name is spelled "governmenttForm" (note the extra "t"), and for the field type, you have "reference govmntForm".

Anthony

mostwanted

unread,
Jul 3, 2017, 3:15:18 AM7/3/17
to web2py-users
It is the actually code Tony, i just changed it after posting it. The table name was initially govmntForm as it is referenced in the governmetFormPages table.

Santiago Cartasegna

unread,
Jul 3, 2017, 10:51:47 AM7/3/17
to web2py-users
Hello,

You should reference the table with the same name that is created so the table sould be like:

db.define_table('governmetFormPages',
                Field('formImage', 'upload'),
                Field('formTitle',requires=IS_NOT_EMPTY()), 
                Field('formName', 'reference governmenttForm'),migrate=False,fake_migrate=True)

Best regards

mostwanted

unread,
Jul 3, 2017, 2:50:45 PM7/3/17
to web2py-users
this is the actual code Santi:


db.define_table('government',
                Field('govName', requires=IS_NOT_EMPTY()),
                Field('tel', requires=IS_NOT_EMPTY()),
                Field('email', requires=IS_NOT_EMPTY()),
                format='%(govName)s',  migrate=False,fake_migrate=True)


db.define_table(govmntForm',

                Field('formImage', 'upload'),
                Field('filledformImage', 'upload'),
                Field('formName',requires=IS_NOT_EMPTY()),
                Field('governmentName', 'reference government'),
                format='%(formName)s',migrate=False,fake_migrate=True)

db.define_table('governmetFormPages',
                Field('formImage', 'upload'),
                Field('formTitle',requires=IS_NOT_EMPTY()),
                Field('formName', 'reference govmntForm'),migrate=False,fake_migrate=True)



Santiago Cartasegna

unread,
Jul 3, 2017, 6:08:44 PM7/3/17
to web2py-users
Go to the database folder backup and delete all the .tables. Perhaps a change was not reflected on the creation of the table.

A similar error ocurred to me and it was on the creation of the id's references on the forange. But your code looks ok

mostwanted

unread,
Jul 5, 2017, 2:34:11 AM7/5/17
to web2py-users
Hey Santi, how do i go about doing that, deleting all the tables in the database backup folder?

Dave S

unread,
Jul 5, 2017, 3:07:46 AM7/5/17
to web2py-users


On Tuesday, July 4, 2017 at 11:34:11 PM UTC-7, mostwanted wrote:
Hey Santi, how do i go about doing that, deleting all the tables in the database backup folder?

The easiest way is to use a command line, and delete the .table files.  In a linux system, and picking the application name from the ticket,

cd [web2pydir]/applications/formulator/databases
rm
*.table

For Windows, the same except substitute del for rm.

(the "backup" mentioned may be a nod to a hosting service, where you might not manipulate the file system directly..  For instance, for heroku, I think you'd actually do a git delete of any dot-table files, but I've only done node.js on heroku, so I'm just waving my hands.)

/dps

Santiago Cartasegna

unread,
Jul 5, 2017, 7:12:22 AM7/5/17
to web...@googlegroups.com
That would do the trick.

The backup means thtat before deleteng the files, you backup the folder!

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/CPmr9UHWvOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Santiago Cartasegna.-

Scheme Lab

unread,
Jul 11, 2017, 5:39:46 PM7/11/17
to web2py-users
I think the filename ends with .sqlite doesnt it?

Dave S

unread,
Jul 11, 2017, 7:31:06 PM7/11/17
to web2py-users


On Tuesday, July 11, 2017 at 2:39:46 PM UTC-7, Scheme Lab wrote:
I think the filename ends with .sqlite doesnt it?

That's the actual database (if you're using sqlite3).  The dot-table files are part of pydal, with schema information used by the driver, etc, and are present for any database engine.

/dps
 

mostwanted

unread,
Jul 18, 2017, 5:20:13 AM7/18/17
to web2py-users
I was able o solve the problem, when i was creating the tables i copied and pasted a code for the 'governmetFormPages' table but to correct the error i had to delete the table and type its code word for word, that solved my problem. thanks for the input guys. Santiago Cartasegna  was right.

Santiago Cartasegna

unread,
Jul 18, 2017, 11:54:56 AM7/18/17
to web...@googlegroups.com
Glad that my idea helped!

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/CPmr9UHWvOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Santiago Cartasegna.-
Reply all
Reply to author
Forward
0 new messages