Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm new to Postgis and Quantum GIS. I created a view defined as

CREATE OR REPLACE VIEW pupr.vstudentmuni AS 
 SELECT m.id AS munid, m.name AS muname, m.geom, s.code, 
    (s.name::text || ' '::text) || s.lastname::text AS fullname
   FROM pupr.municipio m, pupr.student_location l, pupr.student s
  WHERE st_within(l.geom, m.geom) AND s.code = l.code;

in PostgreSQL. Trying to add this view to QGIS returns the following error...

"Invalid layer"..."sql= is an invalid layer and cannot be loaded."

Not sure what the problem is. The table is mentioned in the geommetry_columns table in PostGIS, but I can see a message entry in QGIS that says "No key field for given view". I'm using PostgreSQL 9.2 and Quantum GIS 1.8.0.

share|improve this question
1  
The problem seem to be related to the unique key field, you must have one. I don't know your data, maybe you could explain a bit more about what you want to achieve with your query. How are you opening the Layer in QGIS? Did you choose a key? From what you described, only s.code can be use as unique key. Also, your municipio geometries will be repeated several times for each student, is that what you want? –  Alexandre Neto Jan 22 '13 at 14:49
add comment

1 Answer

When you add a view from PostGIS to QGIS you have to specify the unique ID field. Off to the right of the layer name will be a drop down where you can specify which field to use. If you don't have a unique ID field you can create a fake ID field using the row_number() window function in the query that creates your view.

share|improve this answer
    
This question is related and gives more detail on selecting your unique ID field in the QGIS gui. –  RyanDalton Jan 22 '13 at 15:52
    
And this question will give you more details on how to create a view with a unique id. –  RyanDalton Jan 22 '13 at 15:53
    
Thanks all for the replies. Problem solved using the suggested function. –  ufeliciano Jan 23 '13 at 14:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.