Setting Index Limit in SQLAlchemy

I would like to set the maximum limit for the index in the Column definition or only through the Index constructor, but it seems I have not found a way to achieve it.

Basically, I would like to model this MySQL behavior:

 CREATE TABLE some_table ( id int(11) NOT NULL AUTO_INCREMENT, some_text varchar(2048) DEFAULT NULL, PRIMARY KEY (id), KEY some_text (some_text(1024)), # <- this line ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED; 

In SQLAlchemy, I will have something like:

 class SomeTable(BaseModel): __tablename__ = 'some_table' __seqname__ = 'some_table_id_seq' id = sa.Column(sa.Integer(11), sa.Sequence(__seqname__), primary_key=True) some_text = sa.Column(sa.String(2048), index=True) # <- this line 

but I can’t find anything that would suggest that the index limit can be configured. Sort of:

 some_text = sa.Column(sa.String(2048), index=True, index_length=1024) 

I assume that since this parameter for the Column constructor is just an alias for the Index constructor, is there a custom parameter to include in the Index constructor to enable this setting?

Thanks!

+5
source share
1 answer

I think you can do something like:

 class SomeTable(BaseModel): __tablename__ = 'some_table' __seqname__ = 'some_table_id_seq' __table_args__ = ( sa.Index("idx_some_text", "some_text", mysql_length=1024), ) id = sa.Column(sa.Integer(11), sa.Sequence(__seqname__), primary_key=True) some_text = sa.Column(sa.String(2048)) 

Link: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#index-length

+8
source

Source: https://habr.com/ru/post/1257521/


All Articles