[Qgis-user] Problem seeing/setting Oracle table's primary key

Simon Greener simon at spatialdbadvisor.com
Wed Jan 18 15:23:31 PST 2017


Folks,

I am using QGIS 2.18.3 to connect to Oracle (12.1) table with a primary key:

create table foo (
    fid number(10),
    an_attribute varchar2(10),
    geom sdo_geometry
);

INSERT INTO USER_SDO_GEOM_METADATA (table_name,column_name,DimInfo,srid) VALUES ('FOO', 'GEOM', SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', 5979462.12680312, 6024838.75881869, 0.005), SDO_DIM_ELEMENT('Y', 2085800.17222035, 2131294.00019577, 0.005)), 2872);
commit;

insert into foo(fid,an_attribute,geom) values (1,'D',SDO_GEOMETRY(2003,2872,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(5992342.97800986,2112457.60086077, 5992462.06832285,2112461.87775511, 5992461.15953203,2112486.86130093, 5992342.06921902,2112482.5844066, 5992342.97800986,2112457.60086077)));
insert into foo(fid,an_attribute,geom) values (2,'I',SDO_GEOMETRY(2007,2872,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(5992230.58945511,2112203.40320644, 5992352.0678867,2112207.76605861, 5992351.15909585,2112232.74960443, 5992229.68033619,2112228.38675226, 5992230.58945511,2112203.40320644)));
commit;

create index foo_geom on foo(geom) indextype is mdsys.spatial_index parameters('sdo_indx_dims=2 layer_gtype=MULTIPOLYGON');

-- Create primary key (lazy method) -> automatically creates unique index with same name
alter table foo add constraint foo_pk primary key (fid);

-- Check Unique Index name same as primary constraint
-- Query extracted from SQL tracing of QGIS session.

COLUMN column_name FORMAT A11
SELECT column_name
   FROM all_ind_columns a
        JOIN all_constraints b ON a.index_name=constraint_name AND a.index_owner=b.owner
  WHERE b.constraint_type='P' AND b.owner='TWEED' AND b.table_name='FOO';

/*
COLUMN_NAME
-----------
FID
*/

-- Now we know we have a QGIS compliant primary key name, connect QGIS to Oracle and check if primary key name appears
--> No Primary key displayed.

-- Can anyone tell me why the name of the primary key is not being returned?

-- If any developer is listening, the QGIS query that finds the column_name of the primary key does not cope with situations where a unique index is created BEFORE the primary key is defined as follows:

-- Create primary key using more flexible approach

alter table foo drop constraint foo_pk;
create unique index foo_fid_uidx on foo(fid);
alter table foo add constraint foo_pk primary key (fid) using index foo_fid_uidx;

-- Execute normal QGIS SQL:
SELECT column_name FROM all_ind_columns a JOIN all_constraints b ON a.index_name=constraint_name AND a.index_owner=b.owner WHERE b.constraint_type='P' AND b.owner='TWEED' AND b.table_name='FOO';

--> no rows selected.

-- Actual query should be:
SELECT column_name
 FROM all_ind_columns a
INNER JOIN
all_constraints b
ON ( a.index_owner=b.owner AND a.table_name=b.table_name AND a.index_name=b.index_name )
WHERE b.constraint_type='P' AND b.owner='TWEED' AND b.table_name='FOO';
/*
COLUMN_NAME
-----------
FID
*/

-- Note: ALTER ADD CONSTRAINT PRIMARY KEY without USING INDEX clause creates the index and then inserts its name (same as constraint) into metadata so that index_name is always constraint_name except when USING INDEX clause is used, then index_name in user_constraints is name of user defined unique index.

DELETE FROM USER_SDO_GEOM_METADATA where table_name = 'FOO';
commit;
drop table foo purge;

regards
Simon
-- 
Holder of "2011 Oracle Spatial Excellence Award for Education and Research."
SpatialDB Advice and Design, Solutions Architecture and Programming,
Oracle Database 10g Administrator Certified Associate; Oracle Database 10g SQL Certified Professional
Oracle Spatial, SQL Server, PostGIS, MySQL, ArcSDE, Manifold GIS, FME, Radius Topology and Studio Specialist.
39 Cliff View Drive, Allens Rivulet, 7150, Tasmania, Australia.
Website: www.spatialdbadvisor.com
Email: simon at spatialdbadvisor.com
Voice: +61 362 396397
Mobile: +61 418 396391
Skype: sggreener
Longitude: 147.20515 (147° 12' 18" E)
Latitude: -43.01530 (43° 00' 55" S)
GeoHash: r22em9r98wg
NAC:W80CK 7SWP3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20170119/0b7c3e01/attachment.html>


More information about the Qgis-user mailing list