views:

278

answers:

3

Using the sqlite3 standard library in python 2.6.4, the following query works fine on sqlite3 command line:

select segmentid, node_t, start, number,title  from 
    ((segments inner join position using (segmentid)) 
    left outer join titles using (legid, segmentid)) 
    left outer join numbers using (start, legid, version);

But If I execute it via the sqlite3 library in python I get an error:

>>> conn=sqlite3.connect('data/test.db')
>>> conn.execute('''select segmentid, node_t, start, number,title  from 
((segments inner join position using (segmentid)) left outer join titles using 
(legid, segmentid)) left outer join numbers using (start, legid, version)''')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: cannot join using column start - column not present 
in both tables

The (computed) table on the left hand side of the join appears to have the relevant column because if I check it by itself I get:

>>> conn.execute('''select *  from ((segments inner join position using 
(segmentid)) left outer join titles using 
(legid, segmentid)) limit 20''').description
(('segmentid', None, None, None, None, None, None), ('html', None, None, None, 
None, None, None), ('node_t', None, None, None, None, None, None), ('legid', 
None, None, None, None, None, None), ('version', None, None, None, None, None, 
None), ('start', None, None, None, None, None, None), ('title', None, None, 
None, None, None, None))

My schema is:

CREATE TABLE leg (legid integer primary key,  t char(16), year char(16), 
    no char(16));
CREATE TABLE numbers (
    number char(16), legid integer, version integer, start integer, 
    end integer, prev integer, prev_number char(16), next integer, 
    next_number char(16), primary key (number, legid, version));
CREATE TABLE position (
    segmentid integer, legid integer, version integer, start integer, 
    primary key (segmentid, legid, version));
CREATE TABLE 'segments' 
    (segmentid integer primary key,  html text, node_t integer);
CREATE TABLE titles (legid integer, segmentid integer, title text, 
    primary key (legid, segmentid));
CREATE TABLE versions 
    (legid integer, version integer, primary key (legid, version));
CREATE INDEX idx_numbers_start on numbers (legid, version, start);

I am baffled as to what I am doing wrong. I have tried quitting/restarting both the python and sqlite command lines and can't see what I'm doing wrong. It may be completely obvious.

A: 

You have a table with a funny name:

CREATE TABLE 'segments' 

But I don't think that's the problem. Per request, here's my execution of your query with the 'segments' table recreated as segments:

$ sqlite3 junk.sqlite
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE leg (legid integer primary key,  t char(16), year char(16), 
    no char(16));
CREATE TABLE numbers (
    number char(16), legid integer, version integer, start integer, 
    end integer, prev integer, prev_number char(16), next integer, 
    next_number char(16), primary key (number, legid, version));
CREATE TABLE position (
    segmentid integer, legid integer, version integer, start integer, 
    primary key (segmentid, legid, version));
CREATE TABLE segments (segmentid integer primary key,  html text, node_t integer);
CREATE TABLE titles (legid integer, segmentid integer, title text, 
    primary key (legid, segmentid));
CREATE TABLE versions 
    (legid integer, version integer, primary key (legid, version));
CREATE INDEX idx_numbers_start on numbers (legid, version, start);
sqlite> select segmentid, node_t, start, number,title  from 
   ...>     ((segments inner join position using (segmentid)) 
   ...>     left outer join titles using (legid, segmentid)) 
   ...>     left outer join numbers using (start, legid, version);
Error: ambiguous column name: segmentid
msw
Copying and pasting your schema into my sqlite 3.4.0 and running the exact same query does not give an error for me. What is I don't think it should give an error - unless I am missing something. Do you think it should? Is this a difference between 3.4 and 3.6.22? I am even more baffled now than I was.
Francis Davey
I'm sorry that this was negatively helpful. I'm frankly out of my depth with multiply nested joins, but if I had to guess, sqlite 3.6.22 is probably more semantically "correct" - for the horribly ambiguous language that SQL is. I'm not saying you should prefer it, though.
msw
A: 

SQLite version 3.6.22 -- looks like you need to qualify the "ambiguous column names"...

sqlite> select segmentid, node_t, start, number,title  from
   ...> ((segments inner join position using (segmentid))
   ...> left outer join titles using (legid, segmentid))
   ...> left outer join numbers using (start, legid, version);
Error: ambiguous column name: segmentid

sqlite> select segments.segmentid, node_t, start, number,title  from
   ...> ((segments inner join position using (segmentid))
   ...> left outer join titles using (legid, segmentid))
   ...> left outer join numbers using (start, legid, version);
Error: ambiguous column name: start

sqlite> select segments.segmentid, node_t, numbers.start, number,title  from
   ...> ((segments inner join position using (segmentid))
   ...> left outer join titles using (legid, segmentid))
   ...> left outer join numbers using (start, legid, version);
sqlite> 

The behavior is the same with SQLite version 3.6.23.1

Doug Currie
Which version of sqlite are you using? It might be useful to upgrade to it.
Francis Davey
@Francis, the example above was with 3.6.22
Doug Currie
@Francis, the example above was with 3.6.22 and with 3.6.23.1.
Doug Currie
Thanks. Those are not the errors I've been getting (because I have an older sqlite) and I've managed to "solve" my existing problem, but I'll look out for this when I do move to 3.6. It also shows that sqlite really doesn't get table naming right so something to push for bugfixes at a later stage.
Francis Davey
+1  A: 

A solution (to my problem using the python library) appears to be to introduce an entirely spurious table name:

SELECT legid, version, segmentid, html, node_t, start, number, title 
    from ((segments inner join position using (segmentid))  
    left outer join titles using (legid, segmentid)) as LT 
    left outer join numbers using (start, legid, version);

What I think this does is force sqlite to collect together the names of the left side of the outermost outer join, one of which is "start" and then that gives something for the outermost outer join to operate on. That works for me - it may be that upgrading will introduce more problems rather than remove them, but I'll cross that bridge when it arrives.

Francis Davey