can run insert() with lowercase-t table

Issue #2461 resolved
Mike Bayer repo owner created an issue

too many assumptions being made here

from sqlalchemy import create_engine
from sqlalchemy.sql import table, column

e = create_engine("sqlite://")
conn = e.connect()
conn.execute("""
    create table foo(
        id integer primary key,
        data varchar(50),
        x integer
    )
""")
t1 = table('foo',
            column('id'),
            column('data'),
            column('x')
    )

e.execute(t1.insert(), {"data":"d1", "x":5})

patch for just the autoincrement bug:

diff -r 4cb74452fe551c3d4f0dd305bee1e69dbdccd99a lib/sqlalchemy/sql/expression.py
--- a/lib/sqlalchemy/sql/expression.py  Thu Apr 05 14:31:28 2012 -0400
+++ b/lib/sqlalchemy/sql/expression.py  Sat Apr 07 17:37:22 2012 -0400
@@ -4173,6 +4173,7 @@
     __visit_name__ = 'table'

     named_with_column = True
+    _autoincrement_column = None

     def __init__(self, name, *columns):
         super(TableClause, self).__init__()

also if inline is set, the original plan was we shouldn't care about inserted PKs, but unfortunately it seems like we are getting inserted PKs anyway:

e.execute(t1.insert(inline=True), {"data":"d1", "x":5})

so not sure if we want this in 0.7 or 0.8:

from sqlalchemy import create_engine
from sqlalchemy.sql import table, column
from sqlalchemy import *

# fails
#e = create_engine("sqlite://", echo=True)

# works
e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)

conn = e.connect()
with conn.begin() as t:
    conn.execute("""
        create table foo(
            id SERIAL primary key,
            data varchar(50),
            x integer
        )
    """)
    t1 = table('foo',
                column('id'),
                column('data'),
                column('x')
        )

    T1 = Table('foo', MetaData(),
        Column("id", Integer, primary_key=True),
        Column("data", String),
        Column("x", Integer)
    )
    r = conn.execute(T1.insert(), {"data":"d1", "x":5})
    assert r.inserted_primary_key[0](0) is not None

    # with inline, we aren't doing RETURNING, or
    # pre-executing.  
    r = conn.execute(T1.insert(inline=True), {"data":"d1", "x":5})
    assert r.inserted_primary_key[0](0) is None
    t.rollback()

patch:

diff -r 4cb74452fe551c3d4f0dd305bee1e69dbdccd99a lib/sqlalchemy/engine/default.py
--- a/lib/sqlalchemy/engine/default.py  Thu Apr 05 14:31:28 2012 -0400
+++ b/lib/sqlalchemy/engine/default.py  Sat Apr 07 17:41:42 2012 -0400
@@ -655,6 +655,7 @@

     def post_insert(self):
         if not self._is_implicit_returning and \
+            not self.compiled.inline and \
             self.dialect.postfetch_lastrowid and \
             (not self.inserted_primary_key or \
                         None in self.inserted_primary_key):
diff -r 4cb74452fe551c3d4f0dd305bee1e69dbdccd99a lib/sqlalchemy/sql/expression.py
--- a/lib/sqlalchemy/sql/expression.py  Thu Apr 05 14:31:28 2012 -0400
+++ b/lib/sqlalchemy/sql/expression.py  Sat Apr 07 17:41:42 2012 -0400
@@ -4173,6 +4173,7 @@
     __visit_name__ = 'table'

     named_with_column = True
+    _autoincrement_column = None

     def __init__(self, name, *columns):
         super(TableClause, self).__init__()

Comments (5)

  1. Log in to comment