Invalid SQL produced on autoincremented pk-only table

Issue #789 resolved
Former user created an issue

Setting aside why you would want to do this, inserting into a table with only a primary key will produce an exception using SQLAlchemy on sqlite3.[BR]

table_dicts = Table( "dicts", metadata,
                      Column( "id", String(31), primary_key = True ),
                      #Column( "some_data", String( 255 ) )
                     )

class Dictionary( object ):
    def __init__( self ):
        #self.some_data = "something"
        pass

d = Dictionary()
session.save( d )
session.commit()

produces

sqlalchemy.exceptions.OperationalError: (OperationalError) near ")": syntax error u'INSERT INTO dicts () VALUES ()' [make this code work, uncomment the two commented lines and repeat.
[[BR](]

To)] Changinge the SQL to explicitly include the NULL for the id that the database will fill in works (assuming the primary key is an integer, but I think that's sane):

sqlite> INSERT INTO dicts (id) VALUES (NULL);

Comments (1)

  1. Mike Bayer repo owner

    this is unfortunately a SQLite issue. To work around it, you have to provide an explicit value for the primary key. i.e., in SQLite, primary key values are generated automatically if you leave them absent in the INSERT statement; but then if the table has no other cols, you get INSERT INTO table () VALUES (), hence the error. im not aware of any way to fix this (feel free to reopen if theres some way to insert a single-column table row in sqlite using its auto-pk generation feature).

  2. Log in to comment