I think it would actually be reasonable to say "We support foreign keys
on table.create() in InnoDB for MySQL version >= 4.1.2". Then this
problem goes away.

But to stay inside the 64-char limit and still have fairly meaningful
identifiers, you could do something like this:

def trimunique(s, maxlen=64):
    if len(s) <= maxlen:
        return s
    if maxlen < 6:
        raise ValueError('maxlen must be >=6')
    import random
    randomness = hex(random.randint(0, 0xffffff))[2:]
    return s[:maxlen-len(randomness)] + randomness

In [15]: trimunique('abcdefghijklmnopqrstuvwxyz', 20)
Out[15]: 'abcdefghijklmnfd7ca8'

In [16]: trimunique('abcdefghijklmnopqrstuvwxyz', 20)
Out[16]: 'abcdefghijklmn340bdd'

In [28]: trimunique('abcdefghijklmnopqrstuvwxyz', 20)
Out[28]: 'abcdefghijklmnop6761'



-- Wade





Michael Bayer wrote:
> well, with that naming convention we're going to hit the limit fast on the
> name if a table has big column names.  can we name the foreign key
> something random, like a hex string ?
> 
> Wade Leftwich wrote:
> 
>>Looks good. The index name has to be <= 64 chars, as does every MySQL
>>identifier except an alias, which is <= 255.
>>
>>Note my correction on the syntax. It should be:
>>
>> CREATE TABLE foo (
>>     col1 integer primary key,
>>      <colname> integer,
>>      foreign key <fk>_<colname>_<remote_table_name>_<remote_colname>
>>(<colname>) references <remote_table_name>(<remote_colname>)
>>  ) engine=InnoDB
>>
>>
>>
>>
>>


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to