Search code examples
databaseweb2pydata-access-layer

web2py: FOREIGN KEY constraint failed when it shouldn't


I have this table:

db.define_table('block',
    Field('ore_id', 'reference ore'),
    Field('location', type='integer', required=True, notnull = True),
    Field('x', type='integer'),
    Field('y', type='integer'),
    Field('block_mass_tn', type='double')
)

and this:

db.define_table('block_processing_line_1',
    Field('block_location', 'reference block'),
    Field('processing_time_d', type='double'),
    Field('concentrate_tn', type='double'),
    Field('concentrate_quality', type='double')
)

In the table block I have 100 entries, id and location running from 1-100. When I add a new record to the latter table with block_location being 1, it accepts it, but when I try to add 2 or 3 or so on, it won't accept it and it says "FOREIGN KEY constraint failed". I have other tables, which have that identical field: Field('block_location', 'reference block'), but they don't have any problem with values above 1. What is wrong here?

One other thing that comes in mind is that I had the same problem before with that table and I realized I had made a typo: Field('block_location', 'reference ore'), so I referenced a wrong table and that table ore only had one entry (thus accepting only 1 and nothing else). But now even though I fixed it, the problem persists. Could there be some traces of that former line somewhere in the DAL? I truncated both tables (block and block_processing_line_1after fixing.


Solution

  • @Anthony:

    Have you compiled the app or turned off migrations? If neither, maybe try dropping the block_processing_line_1 table.

    This solved it! I dropped the table and saved, "undropped" the table and saved, and after that it worked as it should. So this line:

    db.block_processing_line_1.drop()