Search code examples
pythonmysqlsqlobject

How to modify the datatype of `id` column for table create using SQLObject


I am trying to create a table using SQLObject with the following schema

class PersonThree(SQLObject):
    firstname = StringCol(length=50)
    lastname = StringCol(length=50)
    age = IntCol()

This by default creates the table with id column whose datatype in int. I want this table to have the datatype of id column to be bigint.

I have tried th following

class PersonThree(SQLObject):
    class sqlmeta:
        idType = 'bigint'
    firstname = StringCol(length=50)
    lastname = StringCol(length=50)
    age = IntCol()

but the above doesn't work.

Is there a way to modify the above schema such that the id column has bigint datatype.

DB - mysql


Solution

  • Currently you cannot do that with SQLObject. First, idType must be a callable that converts Python values to SQL. In short, it must be either int or str.

    Second, MySQLConnection explicitly checks idType to be str or int and creates either TEXT or INT columns.

    The only way currently to do what you want is to create the table outside of SQLObject or alter the table after creation:

    class PersonThree(SQLObject):
        class sqlmeta:
            idType = int
    PersonThree.createTable()
    

    and then

    ALTER TABLE person_three MODIFY COLUMN id BIGINT;
    

    You can try a dirty hack: get SQL from PersonThree.createTableSQL() instead of PersonThree.createTable(), modify it at runtime and execute.

    PS. I'm the current maintainer of SQLObject. A working pull request with a test will be accepted.