10

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ofir
  • 1,565
  • 3
  • 23
  • 41
  • Also the size on integers in MySQL is *mostly* pointless. I'd just use Integer. See http://alexander.kirk.at/2007/08/24/what-does-size-in-intsize-of-mysql-mean/ – Prof. Falken May 16 '13 at 07:48

2 Answers2

8

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)
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Nathan Villaescusa
  • 17,331
  • 4
  • 53
  • 56
  • 6
    That does misses the point of using an ORM, doesn't it? (I need to support Oracle also) – Ofir Apr 04 '13 at 07:38
  • From what I have seen, when one uses the mysql.INTEGER type and connects to a non-mysql database the type reverts to the built in Integer type, so Oracle should still work. I don't think Oracle's INTEGER allows the size to be specified. – Nathan Villaescusa Apr 04 '13 at 08:02
1

There are no arbitrary size integers but SQLAlchemy supports besides Integer also BigInteger and SmallInteger.

https://docs.sqlalchemy.org/en/14/core/type_basics.html#generic-types

Konstantin Smolyanin
  • 17,579
  • 12
  • 56
  • 56