Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

When running the alchemy scaffold on MySQL, an error is generated on table creation #1049

Merged
merged 1 commit into from

3 participants

@cd34

When running the alchemy scaffold on MySQL, an error is generated on table creation:

sqlalchemy.exc.OperationalError: (OperationalError) (1170, "BLOB/TEXT column 'name' used in key specification without a key length") '\nCREATE TABLE models (\n\tid INTEGER NOT NULL AUTO_INCREMENT, \n\tname TEXT, \n\tvalue INTEGER, \n\tPRIMARY KEY (id), \n\tUNIQUE (name)\n)\n\n' ()

MySQL (and MariaDB) doesn't allow an index of more than 255 characters. After
modifying the scaffold and creating a project, the following results:

show create table models;
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| models | CREATE TABLE models (
id int(11) NOT NULL AUTO_INCREMENT,
name text,
value int(11) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY my_index (name(255))
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

A unique index is created on the name field, constrained to 255 characters.

When run on SQLite3:

sqlite> .schema models
CREATE TABLE models (
id INTEGER NOT NULL,
name TEXT,
value INTEGER,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX my_index ON models (name);

the mysql specific constraint is ignored and the table is created with a unique
index as desired.

@cd34 cd34 When running the alchemy scaffold on MySQL an error is generated on t…
…able

creation:

sqlalchemy.exc.OperationalError: (OperationalError) (1170, "BLOB/TEXT column 'name' used in key specification without a key length") '\nCREATE TABLE models (\n\tid INTEGER NOT NULL AUTO_INCREMENT, \n\tname TEXT, \n\tvalue INTEGER, \n\tPRIMARY KEY (id), \n\tUNIQUE (name)\n)\n\n' ()

MySQL (and MariaDB) doesn't allow an index of more than 255 characters. After
modifying the scaffold and creating a project, the following results:

> show create table models;
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                                       |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| models | CREATE TABLE `models` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text,
  `value` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `my_index` (`name`(255))
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

A unique index is created on the name field, constrained to 255 characters.

When run on SQLite3:

sqlite> .schema models
CREATE TABLE models (
	id INTEGER NOT NULL,
	name TEXT,
	value INTEGER,
	PRIMARY KEY (id)
);
CREATE UNIQUE INDEX my_index ON models (name);

the mysql specific constraint is ignored and the table is created with a unique
index as desired.
8320674
@bertjwregeer

Better option would be:

name = Column(String(length=255), unique=True)

@cd34

I agree, however, I didn't want to change the default model as there may have been some historic reasons for it being defined as such.

@mcdonc mcdonc merged commit 8320674 into from
@mcdonc
Owner

Thank you Chris! If your name isn't already in CONTRIBUTORS.txt do you think you could add it to that file in a separate pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 18, 2013
  1. @cd34

    When running the alchemy scaffold on MySQL an error is generated on t…

    cd34 authored
    …able
    
    creation:
    
    sqlalchemy.exc.OperationalError: (OperationalError) (1170, "BLOB/TEXT column 'name' used in key specification without a key length") '\nCREATE TABLE models (\n\tid INTEGER NOT NULL AUTO_INCREMENT, \n\tname TEXT, \n\tvalue INTEGER, \n\tPRIMARY KEY (id), \n\tUNIQUE (name)\n)\n\n' ()
    
    MySQL (and MariaDB) doesn't allow an index of more than 255 characters. After
    modifying the scaffold and creating a project, the following results:
    
    > show create table models;
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table  | Create Table                                                                                                                                                                                                                       |
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | models | CREATE TABLE `models` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` text,
      `value` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `my_index` (`name`(255))
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
    +--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    
    A unique index is created on the name field, constrained to 255 characters.
    
    When run on SQLite3:
    
    sqlite> .schema models
    CREATE TABLE models (
    	id INTEGER NOT NULL,
    	name TEXT,
    	value INTEGER,
    	PRIMARY KEY (id)
    );
    CREATE UNIQUE INDEX my_index ON models (name);
    
    the mysql specific constraint is ignored and the table is created with a unique
    index as desired.
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 1 deletion.
  1. +4 −1 pyramid/scaffolds/alchemy/+package+/models.py
View
5 pyramid/scaffolds/alchemy/+package+/models.py
@@ -1,5 +1,6 @@
from sqlalchemy import (
Column,
+ Index,
Integer,
Text,
)
@@ -20,9 +21,11 @@
class MyModel(Base):
__tablename__ = 'models'
id = Column(Integer, primary_key=True)
- name = Column(Text, unique=True)
+ name = Column(Text)
value = Column(Integer)
def __init__(self, name, value):
self.name = name
self.value = value
+
+Index('my_index', MyModel.name, unique=True, mysql_length=255)
Something went wrong with that request. Please try again.