Setting an index limit in SQLAlchemy
<p>I would like to set up a maximum limit for an index within a <code>Column</code> definition or just through the <code>Index</code> constructor but I don't seem to find a way to achieve it.</p>

<p>Basically, I would like to simulate this MySQL behaviour:</p>

<pre><code>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)), # &lt;- this line
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
</code></pre>

<p>In SQLAlchemy I would have something like:</p>

<pre><code>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) # &lt;- this line
</code></pre>

<p>but I can't find anything that would suggest the limit of the index can be customised. Something like:</p>

<pre><code>some_text = sa.Column(sa.String(2048), index=True, index_length=1024)
</code></pre>

<p>I guess since this option for the <code>Column</code> constructor is just an alias for the <code>Index</code> constructor, is there a custom param to include in the <code>Index</code> constructor to allow this setting?</p>

<p>Thanks!</p>

<p>I think you can do something like: </p>

<pre><code>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))
</code></pre>

<p>Reference:
<a href="http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#index-length" rel="nofollow">http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#index-length</a></p>

python mysql indexing sqlalchemy