• 10338 views
  • 0 comments
  • loading...

Defines, stores and returns a Table object

Tables are defined in the DAL via define_table. The signature for define_table:


















db.define_table('person', Field('name'),

id=id,
rname=None,
redefine=True
common_filter,
fake_migrate,
fields,
format,
migrate,
on_define,
plural,
polymodel,
primarykey,
redefine,
sequence_name,
singular,
table_class,
trigger_name)

Attributes / Arguments

Optionally you can define a Field of type='id' and web2py will use this field as auto-increment id field. This is not recommended except when accessing legacy database tables which have a primary key under a different name. See web2py book for more information.

Smartgrid objects may need to know the singular and plural name of the table. The defaults are smart but these parameters allow you to be specific. See smartgrid for more information.

db.define_table(
   'person',
   Field('name'),
   Field('email'),
   format = '%(name)s',
   singular = 'Person',
   plural = 'Persons',
   )

Smartgrid objects may need to know the singular and plural name of the table. The defaults are smart but these parameters allow you to be specific. See smartgrid for more information.

db.define_table(
   'person',
   Field('name'),
   Field('email'),
   format = '%(name)s',
   singular = 'Person',
   plural = 'Persons',
   )

Tables can be defined only once but you can force web2py to redefine an existing table.
The redefinition may trigger a migration if field content is different.

db.define_table('person', Field('name'))
db.define_table('person', Field('name'), redefine=True)

It is optional but recommended to specify a format representation for records with the format parameter.

The format attribute will be used for two purposes:

To represent referenced records in select/option drop-downs.
To set the db.othertable.person.represent attribute for all fields referencing this table. This means that SQLTABLE will not show references by id but will use the format preferred representation instead.

db.define_table('person', Field('name'), format='%(name)s')

or

db.define_table('person', Field('name'), format='%(name)s %(id)s')

rname sets a database backend name for the table. This makes the web2py table name an alias, and rname is the real name used when constructing the query for the backend. To illustrate just one use, rname can be used to provide MSSQL fully qualified table names accessing tables belonging to other databases on the server:

rname = 'db1.dbo.table1'

primarykey helps support legacy tables with existing primary keys, even multi-part.

db = DAL('sqlite://storage.sqlite')
db.define_table('person',
   Field('name'),
   Field('surname'),
   Field('email'),
   Field('given_name'),
   Field('phone_number'),
   primarykey=['name','surname']
)

migrate sets migration options for the table. See migrate for more information

migrate sets migration options for the table. See migrate for more information

If you define your own Table class as a sub-class of gluon.dal.Table, you can provide it here; this allows you to extend and override methods.

 table_class=MyTable

The name of a custom table sequence (if supported by the database). Can create a SEQUENCE (starting at 1 and incrementing by 1) or use this for legacy tables with custom sequences. Note that when necessary, web2py will create sequences automatically by default (starting at 1).

Relates to sequence_name. Relevant for some backends which do not support auto-increment numeric fields.

For Google App Engine

on_define is a callback triggered when a lazy_table is instantiated, although it is called anyway if the table is not lazy. This allows dynamic changes to the table without losing the advantages of delayed instantiation.

Example:

 db = DAL(lazy_tables=True) 
 db.define_table('person',Field('name'),Field('age','integer'), 
    on_define=lambda table: [ 
           table.name.set_attributes(requires=IS_NOT_EMPTY(),default=''), 
           table.age.set_attributes(requires=IS_INT_IN_RANGE(0,120),default=30), 
# Note this example shows how to use on_define but it is not actually necessary.


Examples

loading...

Sources / Reference

" Web2py Book - Chapter 6 Database Abstraction Level" is licensed under Creative Common License BY-NC-ND 3.0


Comments

loading...

Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)