reddit clone - foreign key constraint failed

188 views
Skip to first unread message

Uri Tamir

unread,
May 14, 2014, 6:01:29 AM5/14/14
to web...@googlegroups.com
Hi all,
I've been following Massimo's video tutorials (which are great! thanks Massimo)

when trying to build the reddit clone and populate it i get a ticket - <class 'sqlite3.IntegrityError'> foreign key constraint failed

the database was populated with users and posts but not with comments. I tried to make sure my code is exactly as the code Massimo showed but /w no success.

any suggestions?

db.define_table('category',
                Field('name', requires = (IS_SLUG(), IS_LOWER(), IS_NOT_IN_DB(db, 'category.name'))))

db.define_table('post',
                Field('category', 'reference category', readable=False, writable=False),
                Field('title', 'string', requires=IS_NOT_EMPTY()),
                Field('url', requires=IS_EMPTY_OR(IS_URL())),
                Field('body', 'text', requires=IS_NOT_EMPTY()),
                Field('votes', 'integer', default=0, readable=False, writable=False),
                auth.signature) # created_on, created_by, modified_on, modified_by, is_active

db.define_table('post_vote',
                Field('post', 'reference post'),
                Field('score', 'integer', default=+1), # numerical representation of votes
                auth.signature)

db.define_table('comm',
                Field('post', 'reference post'),
                Field('parent_comm', 'reference comm'),
                Field('votes', 'integer'),
                Field('body', 'text'),
                auth.signature)

db.define_table('comm_vote',
                Field('comm', 'reference comm'),
                Field('score', 'integer', default=+1), # numerical representation of votes
                auth.signature)

from gluon.contrib.populate import populate
if db(db.auth_user).count()<2:
    populate(db.auth_user, 100)
    db.commit()
if db(db.post).count()<2:
    populate(db.post, 500)
    db.commit()
if db(db.comm).count()<2:
    populate(db.comm, 1000)
    db.commit()


Derek

unread,
May 16, 2014, 7:16:44 PM5/16/14
to web...@googlegroups.com
probably this line here:
Field('parent_comm', 'reference comm')

the parent has to exist before you can reference it. Take a look at line 196+ of populate.py - it tries to get a list of all possible values to reference, but it can't since none exist, so it tries to reference ID zero, but that doesn't exist either, thus you get the foreign key constraint error. There should probably be extra code in there where if it's referencing itself, it should not populate that field.

Chris Simpson

unread,
May 28, 2014, 11:05:06 AM5/28/14
to web...@googlegroups.com

Hi Uri,

Did you ever find a solution to this?

William Chen

unread,
Dec 23, 2014, 3:33:54 AM12/23/14
to
Hi Chris, I had the same problem and found this thread when I was searching for a solution.

The solution is to manually create 2 or 3 comments using appadmin. 

If table B references table A, you need to populate table A before you populate table B, or else there will be an error. In our case, the table comm references to itself in this line:
Field('parent_comm', 'reference comm')

So we need to manually add a couple of new comments using appadmin, before we populate the table comm. 

Hope this helps.

Chris Simpson

unread,
Dec 24, 2014, 8:41:55 AM12/24/14
to web...@googlegroups.com
Hi William,

Thanks for the information!

Regards,




2014-12-23 8:33 GMT+00:00 William Chen <willia...@gmail.com>:
Hi Chris, I had the same problem and found this thread when I was searching for a solution.

The solution is to simply create 1 or more comments using app admin. 

If table B references table A, then you need populate to populate table A before you populate table B, else there will be an error. In our case, the table comm references to itself in this line:
Field('parent_comm', 'reference comm')

So we need to manually add new comments, before we populate the table comm. 

Hope this helps.


On Wednesday, 28 May 2014 23:05:06 UTC+8, Chris Simpson wrote:

--
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/9L1Ycuw7ZPo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

YingJun li

unread,
Jun 21, 2017, 4:18:02 PM6/21/17
to web2py-users
This is help, thank you.
Reply all
Reply to author
Forward
0 new messages