Search code examples
pythonweb2py

referencing a table which is defined after the definition of the first table in web2py


    db.define_table("devices",
            Field('user_id','reference users'),#   THIS PRODUCES AN ERROR 
            Field('energyConsumed','integer'),
            Field('device_password','password'),
            Field('date_of_measure','date')
            );
db.define_table("users",
            Field('device_id','reference devices')
);

I am unable to use 'reference users' in the first table because it has not been defined before that tables definition . How can I reference table which is defined only later on .


Solution

  • You can not reference to table which is not defined. So you have to use alternative syntax.

    Use IS_IN_DB validator for this.

    IS_IN_DB(db, 'users.id')
    

    This is already answered here:

    https://stackoverflow.com/a/38948788/4065350 https://groups.google.com/forum/#!msg/web2py/yNca8bq0HmM/DmVjCPrODQAJ