On Friday, 29 August 2014 14:22:56 UTC+1, Gary Cowell wrote:
>
> Hello
>
> I have the following in models:
>
> db.define_table('senders',
>     Field('sender','string',length=80,label="Sender"))
>
> db.define_table('documents',
>     
> Field('description','text',requires=IS_LENGTH(256,1),label="Description"),
>     Field('header_date','date',label="Header Date"),
>     Field('received_date','date',label="Received Date"),
>     Field("sent_by",db.senders,label="Sent By",requires=IS_IN_DB(db, 
> 'senders.sender')))
>
>
>
> So I want some 'senders', with a description 'sender' and some 
> 'documents'. Each 'documents' has one 'senders', a 'senders' has many 
> 'documents'.
>
> I added the requres=IS_IN_DB to enable a SQLFORM to provide a drop down 
> list of SENDERS when I create a new document. This works but ...
>
> We get this:
>
> <class 'sqlite3.IntegrityError'> FOREIGN KEY constraint failed
>
> When we create the DOCUMENT, even though it specifies a SENDER from the 
> dropdown
>
> Is this because the drop down is senders.sender but the RI constraint is 
> on senders.id ?
>
> I'm not a fan of surrogate keys, I must say. Traditionally I would have 
> defined SENDERS as SENDER_CODE, SENDER_NAME and given a meaningful code, 
> such as 
>
> NWAT, Northumbrian Water
>
> instead of
>
> 1,Northumbrian Water
>
> which is what I have now.
>
> Anyway, what have I messed up in my model/form, please? I can't see how to 
> change it to fix this
>
> Form is built with this controller code:
>
> @auth.requires_login()
> def newdocument():
>
>     form = SQLFORM(db.documents).process()
>
>     if form.accepted:
>         response.flash = 'new record inserted'
>
>     dochead = {'documents.description':'Description', 
> 'documents.header_date':'Header Date', 'documents.received_date':'Received 
> Date', 'documents.sent_by':'Sender',}
>         
>     # and get a list of all documents
>     records = SQLTABLE(db().select(db.documents.ALL), truncate=80, headers 
> = dochead )
>
>     return dict(form=form, records=records)
>
> View:
>
> {{extend 'layout.html'}}
> <h1>Documents</h1>
> {{=form}}
> <h2>Current Documents</h2>
> {{=records}}
>
> Thanks for any assistance
>



For reference: I fixed this by reading tfm. Model is now:

db.define_table('senders',
    Field('sender','string',length=80,label="Sender"),
    format = '%(sender)s')

db.define_table('documents',
    
Field('description','text',requires=IS_LENGTH(256,1),label="Description"),
    Field('header_date','date',label="Header Date"),
    Field('received_date','date',label="Received Date"),
    Field("sent_by",db.senders),
    format = '%(description)s') 

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to