Hi, your shown code is incomplete. It seems you are doing an insert or 
update to the db.

This "FOREIGN KEY constriaint failed" is meaning that in one of your 
referenced fields [db.student.guardian, db.student.class_name] you tried to 
reference a registry that doesn't exist on the parent tables [db.guardian, 
db.class_name]

As an example, *db.student.insert(db.guardian=2)* will raise this error if 
*db.guardian(2)==None*  (doesn't exist).




On Wednesday, October 25, 2017 at 9:55:28 AM UTC+2, mostwanted wrote:
>
>
>
> *WHEN I TRY ENTERING A STUDENT'S DETAILS AND SELECTING THEIR GUARDIAN AS 
> FROM THE DROP DOWN MENU AS REFERNCED I GET A FOREIGN CONSTRAINT FAILED 
> ERROR MESSAGEMY TABLES*
>
> db.define_table('guardian',
>                 Field('surname', requires=IS_NOT_EMPTY()),
>                 Field('name', requires=IS_NOT_EMPTY()),
>                 Field('contact_number'),
>                 format='%(surname)s')
>                 
> db.define_table('student',
>                 Field('surname', requires=IS_NOT_EMPTY()),
>                 Field('name', requires=IS_NOT_EMPTY()),
>                 Field('photo', 'upload', requires=IS_NOT_EMPTY()),
>                 Field('guardian', 'reference guardian'),
>                 Field('class_name', 'reference student_class'),
>                 Field('dob', 'date', requires=IS_NOT_EMPTY()),
>                 format='%(surname)s')
>
> *MY CONTROLLER*
>
> def show_students():
>     show_students=db(db.student).select(db.student.ALL)
>     return locals()
>
>
>
> *MY VIEW*<div id="container">
>     <h2>SELECT A STUDENT TO VIEW THEIR PROFILE</h2>
> <table>
> <tr>
>     <th>SURNAME</th>
>     <th>NAME</th>
>     <th>CLASS</th>
>     <th>GUARDIAN</th>
> </tr>
> {{for students in show_students:}}
>     <tr>
>         <td>{{=A('VIEW', _class="glyphicon glyphicon-user", 
> _href=URL('student_profile', args=students.id))}} | 
>          {{=students.surname}}</td>
>         <td>{{=students.name}}</td>
>         <td>{{=students.class_name.class_name}}</td>
>         <td>{{=students.guardian.guardian}}</td>
>     </tr>
>     {{pass}}
> </table>
> </div>
>
>
> *MY TRACEBACK ERROR*
> <class 'sqlite3.IntegrityError'> FOREIGN KEY constraint failed 
>
>
>
> Traceback (most recent call last):
>   File "/home/mostwanted/web2py/gluon/restricted.py", line 219, in restricted
>     exec(ccode, environment)
>   File "/home/mostwanted/web2py/applications/SRMS/controllers/appadmin.py" 
> <http://127.0.0.1:8000/admin/default/edit/SRMS/controllers/appadmin.py>, line 
> 696, in <module>
>   File "/home/mostwanted/web2py/gluon/globals.py", line 409, in <lambda>
>     self._caller = lambda f: f()
>   File "/home/mostwanted/web2py/applications/SRMS/controllers/appadmin.py" 
> <http://127.0.0.1:8000/admin/default/edit/SRMS/controllers/appadmin.py>, line 
> 147, in insert
>     if form.accepts(request.vars, session):
>   File "/home/mostwanted/web2py/gluon/sqlhtml.py", line 1862, in accepts
>     self.vars.id = self.table.insert(**fields)
>   File "/home/mostwanted/web2py/gluon/packages/dal/pydal/objects.py", line 
> 734, in insert
>     ret = self._db._adapter.insert(self, row.op_values())
>   File "/home/mostwanted/web2py/gluon/packages/dal/pydal/adapters/base.py", 
> line 486, in insert
>     raise e
> IntegrityError: FOREIGN KEY constraint failed
>
> Error snapshot [image: help] 
> <http://127.0.0.1:8000/admin/default/ticket/SRMS/127.0.0.1.2017-10-25.09-37-00.f00cf3b9-52ee-46d9-867a-5dae7b823f93#>
>  
>
> <class 'sqlite3.IntegrityError'>(FOREIGN KEY constraint failed) 
>
> inspect attributes 
> Frames 
>    
>    - 
>    
>    *File /home/mostwanted/web2py/gluon/restricted.py in restricted at 
>    line 219* code arguments variables 
>    - 
>    
>    *File 
>    /home/mostwanted/web2py/applications/SRMS/controllers/appadmin.py in 
>    <module> at line 696* code arguments variables 
>    - 
>    
>    *File /home/mostwanted/web2py/gluon/globals.py in <lambda> at line 409* 
>    code arguments variables 
>    - 
>    
>    *File 
>    /home/mostwanted/web2py/applications/SRMS/controllers/appadmin.py in 
> insert 
>    at line 147* code arguments variables 
>    - 
>    
>    *File /home/mostwanted/web2py/gluon/sqlhtml.py in accepts at line 1862* 
>    code arguments variables 
>    - 
>    
>    *File /home/mostwanted/web2py/gluon/packages/dal/pydal/objects.py in 
>    insert at line 734* code arguments variables 
>    - 
>    
>    *File 
>    /home/mostwanted/web2py/gluon/packages/dal/pydal/adapters/base.py in 
> insert 
>    at line 486* code arguments variables 
>    Function argument list 
>    
>    (self=<pydal.adapters.sqlite.SQLite object>, table=<Table student (id, 
>    surname, name, photo, guardian, class_name, dob)>, 
>    fields=[(<pydal.objects.Field object>, 'Leburu'), (<pydal.objects.Field 
>    object>, 'Percy'), (<pydal.objects.Field object>, '2010-10-05'), 
>    (<pydal.objects.Field object>, 5), (<pydal.objects.Field object>, 21), 
>    (<pydal.objects.Field object>, 
>    'student.photo.a21f46af61cba907.706963322e6a7067.jpg')])
>    Code listing 
>    
>    481.
>    482.
>    483.
>    484.
>    485.
>    486.
>    
>    487.
>    488.
>    489.
>    490.
>    
>                self.execute(query)
>            except:
>                e = sys.exc_info()[1]
>                if hasattr(table, '_on_insert_error'):
>                    return table._on_insert_error(table, fields, e)
>                raise e
>    
>            if hasattr(table, '_primarykey'):
>                pkdict = dict([
>                    (k[0].name, k[1]) for k in fields
>                    if k[0].name in table._primarykey])
>    
>    Variables 
>    e IntegrityError('FOREIGN KEY constraint failed',) 
>    
> Context 
>
> locals request session response 
> In file: /home/mostwanted/web2py/applications/SRMS/controllers/appadmin.py 
>
> 1.
>
> <code object <module> at 0xb6e606e0, file 
> "/home/mostwanted/web2py/applications/SRMS/controllers/appadmin.py", line 7>
>
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to