Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlAlchemy: How to make a LONGBLOB column in mysql?

Using sqlalchemy with mysql-python, I have this table:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Index, LargeBinary
Base = declarative_base()
class Tbl(Base):
     __tablename__ = 'tbl'
     ...
     data = Column(LargeBinary())

However, when I create this table (using Base.metadata.create_all(engine)), and then DESCRIBE tbl; in mysql, I get this:

mysql> describe logs;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
...
| data         | blob        | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+

Expected result: I want this to be a longblob in mysql, and not a blob which is limited to 64kB

like image 756
Knio Avatar asked May 04 '17 19:05

Knio


People also ask

Can I use SQLAlchemy with MySQL?

SQLAlchemy supports MySQL starting with version 5.0. 2 through modern releases, as well as all modern versions of MariaDB.

What is Longblob?

Description. A BLOB column with a maximum length of 4,294,967,295 bytes or 4GB (232 - 1). The effective maximum length of LONGBLOB columns depends on the configured maximum packet size in the client/server protocol and available memory.

What is nullable SQLAlchemy?

From SQLAlchemy docs: nullable – If set to the default of True, indicates the column will be rendered as allowing NULL, else it's rendered as NOT NULL. This parameter is only used when issuing CREATE TABLE statements.

What is PickleType?

PickleType. Holds Python objects, which are serialized using pickle. SchemaType. Mark a type as possibly requiring schema-level DDL for usage.


1 Answers

Using:

data = Column(LargeBinary(length=(2**32)-1))

Causes LargeBinary to create a longblob type

like image 88
Knio Avatar answered Sep 23 '22 12:09

Knio