2017-01-11 34 views
0

我正在創建試圖從Python中創建Postgres中的函數。我與postgres數據庫的連接使用Psycopg 2,並在其他情況下成功連接。代碼:在Python中創建Postgres函數

pg_cursor.execute('CREATE OR REPLACE FUNCTION %s.fix_geometry(geometry) \ 
    RETURNS SETOF geometry AS\ 
    $BODY$\ 
     SELECT geom the_geom\ 
     FROM \ 
       (\ 
       SELECT (st_dump(st_buffer(st_snaptogrid(st_makevalid(the_geom), 0.5), 0))).geom\ 
       FROM (SELECT geom the_geom FROM st_dump(st_snaptogrid($1, 0.5))) a\ 
       ) b\ 
     WHERE geometrytype(geom) = \'POLYGON\' AND st_area(geom) >= 0.01;\ 

    $BODY$\ 
     LANGUAGE sql VOLATILE\ 
     COST 100\ 
     ROWS 1000;' %(schema)) #line 84 

pg_connection.commit() 
pg_cursor.execute('ALTER FUNCTION %s.fix_geometry(geometry) OWNER TO analysis' % (schema)) 
pg_connection.commit() 

我得到一個錯誤:

line 84, in
ROWS 1000;' %(schema))
ProgrammingError: type geometry does not exist

當我運行在pgAdmin的它的代碼執行成功。我錯過了什麼?

Python 2.7,Postgres 9.3

回答

0

事實證明,錯誤只是在提供的代碼片段之外。 Postgre在公共模式內部有幾何類型,當我爲這段代碼定義搜索路徑時,我只定義了我工作的模式;公衆不包括在內。所以....

pg_cursor.execute('set search_path = %s, public' % (schema)) 
pg_connection.commit()