Asking support for MySQL create/alter column "AFTER" keyword

184 views
Skip to first unread message

Alfonso Serra

unread,
Dec 7, 2017, 11:18:35 AM12/7/17
to web...@googlegroups.com
Hi everybody.

I would like to ask for support for the create column "AFTER" another column keyword so whenever we change something in our models the order of the columns in the database is respected. I know this is a cosmetic change but migrations wont mess up column order this way. In large databases with many tables and more columns this may become a reason to disable automatic migrations.

Example:
ALTER TABLE `bookings` ADD COLUMN `total_invoiced` DOUBLE NULL AFTER `rooms`;

Please let us know if its worth the new feature.

Thanks
King Regards.

Massimo Di Pierro

unread,
Dec 16, 2017, 4:47:56 PM12/16/17
to web2py-users
I would support to change. Can you help make it happen?

Alfonso Serra

unread,
Dec 18, 2017, 4:59:52 AM12/18/17
to web2py-users
I would love to, have some time in December to spend in personal projects.
Any directions or shall i start researching the source code?

Massimo Di Pierro

unread,
Dec 24, 2017, 11:25:35 AM12/24/17
to web2py-users
look in web2py/gluon/packages/dal/pydal and search for "ALTER" in all files there and subfolders. It wiil give you and idea of how the logic is implemented.

Alfonso Serra

unread,
Jan 23, 2018, 7:01:33 PM1/23/18
to web2py-users
This works only on newly created fields over MySQL. Moving fields around on your model wont reorder them.

Version 2.15.3-stable+timestamp.2017.08.07.12.51.45
migrator.py line 364
                    field_position = sql_fields[key]['sortable']
                    after
= "FIRST"
                   
if field_position > 1:
                       
for f in sql_fields:
                           
if sql_fields[f]['sortable'] == field_position - 1:
                                after
= "AFTER `%s`" % f
                               
break
                    query
= ['ALTER TABLE %s ADD %s %s %s;' % (
                        table
._rname, sql_fields[key]['rname'],
                        sql_fields_aux
[key]['sql'].replace(', ', new_add), after)]

Theres no way to implement column reorder because the db is not being asked for the table schema, this information is read from the migration files. This function only checks whther the fields on your model are in the migration files, in no particular order. It would be nice if there were a way.

migrator.py

Alfonso Serra

unread,
Jul 22, 2021, 11:10:50 PM7/22/21
to web2py-users
Hi Massimo

I have created standalone functions that reorders table fields and support for indexing regular fields by adding "indexed" to the Field signature at objects.py
Before trying the script please add indexed=False to the Field signature and self.indexed = indexed in objects.py, or overwrite with the attached objects.py file.

Its naive and in progress as it only supports common mysql types.

Please let me know how it looks, ill try to integrate this into pydal on my own but i still have to study how.

Thanks
Best Regards.
db_functions.zip
Reply all
Reply to author
Forward
0 new messages