Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy Integer column size

I saw somewhere that you can define a column size for Integer columns (e.g. Integer(20), Integer(10), etc.) but for some reason, it seems that sqlalchemy ignore those sizes in the create table query it produces through create_all():

class Job(Base):
    __tablename__ = "t_job"

    id = Column(Integer(20), Sequence('%s_id_seq' % __tablename__), primary_key=True, nullable=False)
    name = Column(String(30))
    company_id = Column(Integer(20), ForeignKey("t_company.id", ondelete="CASCADE"), nullable=False)

Produces the following query:

CREATE TABLE t_job (
        id INTEGER NOT NULL AUTO_INCREMENT,
        name VARCHAR(30),
        company_id INTEGER NOT NULL,
        PRIMARY KEY (id),
        FOREIGN KEY(company_id) REFERENCES t_company (id) ON DELETE CASCADE
)

If that's not a proper way to do this, what is?

like image 374
Ofir Avatar asked Apr 04 '13 07:04

Ofir


1 Answers

This functionality was deprecated in version 0.7.

If you are using MySQL, you can use the mysql.INTEGER datatype:

from sqlalchemy.dialects import mysql 

class Job(Base):
    __tablename__ = "t_job"

    id = Column(mysql.INTEGER(20), Sequence('%s_id_seq' % __tablename__), primary_key=True, nullable=False)
    name = Column(String(30))
    company_id = Column(Integer(20), ForeignKey("t_company.id", ondelete="CASCADE"), nullable=False)
like image 194
Nathan Villaescusa Avatar answered Oct 25 '22 11:10

Nathan Villaescusa