Cannot create tables in SQLite

Issue #499 resolved
Former user created an issue

Hi, starting with new version of SQLAlchemy, 0.3.5, the following code failed to create a table in sqlite. The code was working with version 0.3.4. Also, the code works fine with MySQL.

#!/usr/bin/env python

# system modules
import sys, string, time, types
from   sqlalchemy import *

engine = create_engine('sqlite:///sqlite.db', echo=True)
#
tableList = [= Sequence(name='seq_user',start=1,increment=True)
t_user = Table('t_user', engine,
  Column('id', Integer, Sequence('seq_user'), nullable = False, primary_key = True),
  Column('userid', String(60), nullable = False, unique=True)
)
tableList.append(t_user)
#
seq_command = Sequence(name='seq_command',start=1,increment=True)
t_command = Table('t_command', engine,
  Column('id', Integer, Sequence('seq_command'), nullable = False, primary_key = True),
  Column('command', String(1000))
)
tableList.append(t_command)
#
seq_history = Sequence(name='seq_history',start=1,increment=True)
t_history = Table('t_history', engine,
  Column('id', Integer, Sequence('seq_history'), nullable = False, primary_key = True),
  Column('userid', Integer, ForeignKey("t_user.id"), nullable = False),
  Column('cmdid', Integer, ForeignKey("t_command.id"), nullable = False),
  Column('date', Date, onupdate=func.current_timestamp()),
  Column('time', Time, onupdate=func.current_timestamp())
)
tableList.append(t_history)
#
# main
#
if __name__ == "__main__":
    tableList.reverse()
    for table in tableList:
        print "Drop table",table
        try:
           table.drop()
        except:
           pass
    tableList.reverse()
    for table in tableList:
        print table,type(table)
        try:
           table.create()
        except:
           sys.excepthook(sys.exc_info()[0](]
#
seq_user),sys.exc_info()[1](1),sys.exc_info()[2](2))
           raise "Fail to create %s"%table.name

The traceback: Traceback (most recent call last): File "/Users/vk/testSQLA.py", line 49, in ? table.create() File "/opt/local/lib/python2.4/site-packages/sqlalchemy/schema.py", line 326, in create self.engine.create(self, checkfirst=checkfirst) File "/opt/local/lib/python2.4/site-packages/sqlalchemy/engine/base.py", line 402, in create self._run_visitor(self.dialect.schemagenerator, entity, connection=connection, kwargs) File "/opt/local/lib/python2.4/site-packages/sqlalchemy/engine/base.py", line 426, in _run_visitor element.accept_schema_visitor(visitorcallable(self, conn.proxy, connection=conn, kwargs)) File "/opt/local/lib/python2.4/site-packages/sqlalchemy/schema.py", line 279, in accept_schema_visitor return visitor.visit_table(self) File "/opt/local/lib/python2.4/site-packages/sqlalchemy/ansisql.py", line 635, in visit_table self.append("\t" + self.get_column_specification(column, first_pk=column.primary_key and not first_pk)) File "/opt/local/lib/python2.4/site-packages/sqlalchemy/databases/sqlite.py", line 284, in get_column_specification colspec = self.preparer.format_column(column) + " " + column.type.engine_impl(self.engine).get_col_spec() File "/opt/local/lib/python2.4/site-packages/sqlalchemy/types.py", line 51, in get_col_spec raise NotImplementedError() NotImplementedError Traceback (most recent call last): File "/Users/vk/testSQLA.py", line 52, in ? raise "Fail to create %s"%table.name Fail to create t_history

Thanks, Valentin.

Comments (1)

  1. Mike Bayer repo owner

    based on looking at your stack trace, I suspect you have to uninstall an old version of SQLAlchemy, since setuptools will create a path something like /site-packages/SQLAlchemy-0.3.5-py2.4.egg/sqlalchemy/__init__.pyc for most versions. Additionally, line 51 of types.py is nowhere near the code described by the traceback.

    using SA 0.3.5 as well as the latest trunk (+ very recent sqlite/pysqlite), I get this output:

    Drop table t_history
    2007-03-03 13:03:50,621 INFO sqlalchemy.engine.base.Engine.0x..f0 
    DROP TABLE t_history
    2007-03-03 13:03:50,622 INFO sqlalchemy.engine.base.Engine.0x..f0 None
    2007-03-03 13:03:50,624 INFO sqlalchemy.engine.base.Engine.0x..f0 ROLLBACK
    Drop table t_command
    2007-03-03 13:03:50,627 INFO sqlalchemy.engine.base.Engine.0x..f0 
    DROP TABLE t_command
    2007-03-03 13:03:50,628 INFO sqlalchemy.engine.base.Engine.0x..f0 None
    2007-03-03 13:03:50,630 INFO sqlalchemy.engine.base.Engine.0x..f0 ROLLBACK
    Drop table t_user
    2007-03-03 13:03:50,633 INFO sqlalchemy.engine.base.Engine.0x..f0 
    DROP TABLE t_user
    2007-03-03 13:03:50,634 INFO sqlalchemy.engine.base.Engine.0x..f0 None
    2007-03-03 13:03:50,635 INFO sqlalchemy.engine.base.Engine.0x..f0 ROLLBACK
    t_user <class 'sqlalchemy.schema.Table'>
    2007-03-03 13:03:50,639 INFO sqlalchemy.engine.base.Engine.0x..f0 
    CREATE TABLE t_user (
            id INTEGER NOT NULL, 
            userid VARCHAR(60) NOT NULL, 
            PRIMARY KEY (id), 
             UNIQUE (userid)
    )
    
    
    2007-03-03 13:03:50,641 INFO sqlalchemy.engine.base.Engine.0x..f0 None
    2007-03-03 13:03:50,649 INFO sqlalchemy.engine.base.Engine.0x..f0 COMMIT
    t_command <class 'sqlalchemy.schema.Table'>
    2007-03-03 13:03:50,654 INFO sqlalchemy.engine.base.Engine.0x..f0 
    CREATE TABLE t_command (
            id INTEGER NOT NULL, 
            command VARCHAR(1000), 
            PRIMARY KEY (id)
    )
    
    
    2007-03-03 13:03:50,656 INFO sqlalchemy.engine.base.Engine.0x..f0 None
    2007-03-03 13:03:50,661 INFO sqlalchemy.engine.base.Engine.0x..f0 COMMIT
    t_history <class 'sqlalchemy.schema.Table'>
    2007-03-03 13:03:50,686 INFO sqlalchemy.engine.base.Engine.0x..f0 
    CREATE TABLE t_history (
            id INTEGER NOT NULL, 
            userid INTEGER NOT NULL, 
            cmdid INTEGER NOT NULL, 
            date DATE, 
            time TIME, 
            PRIMARY KEY (id), 
             FOREIGN KEY(userid) REFERENCES t_user (id), 
             FOREIGN KEY(cmdid) REFERENCES t_command (id)
    )
    
    
    2007-03-03 13:03:50,689 INFO sqlalchemy.engine.base.Engine.0x..f0 None
    2007-03-03 13:03:50,700 INFO sqlalchemy.engine.base.Engine.0x..f0 COMMIT
    
  2. Log in to comment