pydal add_field?

50 views
Skip to first unread message

Lucas

unread,
Feb 9, 2026, 10:29:06 AMFeb 9
to py4web
hello one and all,

I do not think this exists but can an add_field('field_name', 'type', default=xx, requires=xx, comment=xx, etc.) function be added to db.tables['table_name']?

this would also make it easier to add custom fields after auth initiated auth_user, auth_user_tag_groups, etc.

thank you in advance, lucas

Dave S

unread,
Feb 9, 2026, 5:08:12 PMFeb 9
to py4web
I thought this capability already existed, without requiring a new function.  Can't you simply do

table.fields["new_field"] = FIELD(('type': 'text'),('default': xx), ('requires': 'IS_XX'),('comment':xx))

ISTR doing this in web2py.

/dps


Massimo DiPierro

unread,
Feb 16, 2026, 2:35:09 PMFeb 16
to py4web
This does not exist for a reason. db.define_table(...) checks for differences between the current table and the previous table and performs an alter table. If you where to do:

1) db.define_table("mytable", Field("name"))
2) db[mytable].add_field(Field("new_field"))

when restarting py4web and executing 1, since the new_field is not there (not until executing 2) but it was there before (previous execution of py4web), it will alter table and delete your data for that column. Then when executing 2 it would recreate it empty. In old SQLITE versions this would not be a problem simply because it does not support ALTER TABLE... DROP but still it is not safe

Dave S

unread,
Feb 19, 2026, 5:44:44 PM (12 days ago) Feb 19
to py4web
What I describe is a form thing, and now I'm doubting that I've done it to a real table in web2py.  And per Massimo's point, I've gotten things backwards when adding fields in db.py, so I would definitely have had "migration" problems if I wanted a new permanent field and tried doing it from a controller.
 

/dps

-d
 
Reply all
Reply to author
Forward
0 new messages