text name of table in a define_table statement not visible as an object name on the DB

24 views
Skip to first unread message

Michael Cowen

unread,
Apr 23, 2018, 9:19:51 PM4/23/18
to web2py-developers
I am completely new to web2py and just trying to compare it to some other options.  So in my database model I have some code to create a table that works great.  I am using postgresql for the backend.  I can go into the database and see the table just fine.  I can insert records by hand directly into postgresql and query them in code with executesql.  The actual code in the database model (.../models/db.py) is as follows


    # Connect to a local postgres instance
    db = DAL("postgres://user@localhost/DBName")

    # Create some fields ...
    _parent  = Field('parent',  type='integer'  )
    _name    = Field('name',    type='string'   )
    _author  = Field('author',  type='string'   )
    _created = Field('created', type='datetime' )

    # Create a table from those fields
    db.define_table( 'project_common', _parent, _name, _author, _created )



In another file I reference the defined table as an object as follows ...

    # Connect to a local postgres instance 
    db = DAL("postgres://user@localhost/DBName")

    # Insert or Update the row
    db.project_common.update_or_insert( \
        db.project_common.id == _id,    \
        parent  = _parent,              \
        name    = _name,                \
        author  = _author,              \
        created = _create )

At run-time the update_or_insert statement fails claiming ...

    ... AttributeError: 'DAL' object has no attribute 'project_common

I am a bit confused because this looks like it is straight out of the documentation.  Do I have to re-create the define_table statement in every file to make it work.   I can successfully run executesql() statements all day long against the db object, but that locks me into the SQL syntax of my backend DB.  I want to use DAL for all that it offers, but I am missing something fundamental.  Is there a common mistake newbies make.

Anthony

unread,
Apr 23, 2018, 9:25:05 PM4/23/18
to web2py-developers
In the future, please post questions like this to the web2py-users group (https://groups.google.com/forum/#!forum/web2py). This group is for discussion about the development of web2py.


On Monday, April 23, 2018 at 9:19:51 PM UTC-4, Michael Cowen wrote:
I am completely new to web2py and just trying to compare it to some other options.  So in my database model I have some code to create a table that works great.  I am using postgresql for the backend.  I can go into the database and see the table just fine.  I can insert records by hand directly into postgresql and query them in code with executesql.  The actual code in the database model (.../models/db.py) is as follows


    # Connect to a local postgres instance
    db = DAL("postgres://user@localhost/DBName")

    # Create some fields ...
    _parent  = Field('parent',  type='integer'  )
    _name    = Field('name',    type='string'   )
    _author  = Field('author',  type='string'   )
    _created = Field('created', type='datetime' )

    # Create a table from those fields
    db.define_table( 'project_common', _parent, _name, _author, _created )



In another file I reference the defined table as an object as follows ...

    # Connect to a local postgres instance 
    db = DAL("postgres://user@localhost/DBName")

Do not redefine the db object -- when you do that, you lost any tables that were defined on the original db object.

Anthony
Reply all
Reply to author
Forward
0 new messages