【发布时间】:2022-01-09 18:17:43
【问题描述】:
我有下面发布的表。如图所示,表GridCell 的所有行都作为复合主键,它们是表GridCellOpDependentParticular 中的外键。这同样适用于表OpDependentParticular。
对于表GridCell,geometryOfCellRepresentativeToTreatment 和geometryOfCellRepresentativeToBuffer 两列的初始值设置为POLYGON EMPTY 或010300000000000000。
创建表后,我将更新后两列如下:
sql = '''
UPDATE GridCell set
geometryOfCellRepresentativeToTreatment = ST_GeomFromGeoJSON(fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON)
WHERE
geometryOfCellRepresentativeToTreatment = 'POLYGON EMPTY' and fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON <> '' and fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON IS NOT NULL;
UPDATE GridCell SET
geometryOfCellRepresentativeToBuffer = ST_GeomFromGeoJSON(fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON)
WHERE
geometryOfCellRepresentativeToBuffer = 'POLYGON EMPTY' and fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON <> '' and fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON IS NOT NULL;
'''.format()
self.cur.execute(sql)
self.conn.commit()
我想要实现的是使表GridCellOpDependentParticular 中的两列fk_gridCell_geometryOfCellRepresentativeToTreatment geometry 和fk_gridCell_geometryOfCellRepresentativeToBuffer geometry 在我执行后自动更新
上述更新语句。
我搜索了答案,但没有找到仅更新复合外键中的某些列而不指定所有外键的答案
代码:
create table if not exists GridCell(
fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON jsonb,
fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON jsonb,
geometryOfCellRepresentativeToTreatment geometry,
geometryOfCellRepresentativeToBuffer geometry,
fk_site_selectedSiteID text,
fk_OpIndependentParticular_isTreatment boolean,
fk_OpIndependentParticular_isBuffer boolean,
fk_OpIndependentParticular_distanceFromCPOfTreatmentToNearestEdge float8,
fk_OpIndependentParticular_distanceFromCPOfBufferToNearestEdge float8,
foreign key (fk_site_selectedSiteID) references Site(selectedSiteID),
foreign key (fk_OpIndependentParticular_isTreatment,fk_OpIndependentParticular_isBuffer,fk_OpIndependentParticular_distanceFromCPOfTreatmentToNearestEdge,fk_OpIndependentParticular_distanceFromCPOfBufferToNearestEdge)
references OpIndependentParticular(isTreatment,isBuffer,distanceFromCPOfTreatmentToNearestEdge,distanceFromCPOfBufferToNearestEdge),
primary key (fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON,fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON,geometryOfCellRepresentativeToTreatment,
geometryOfCellRepresentativeToBuffer,fk_site_selectedSiteID,fk_OpIndependentParticular_isTreatment,fk_OpIndependentParticular_isBuffer,fk_OpIndependentParticular_distanceFromCPOfTreatmentToNearestEdge,
fk_OpIndependentParticular_distanceFromCPOfBufferToNearestEdge)
/*
* UNIQUE (fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON,fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON,geometryOfCellRepresentativeToTreatment,geometryOfCellRepresentativeToBuffer),
* UNIQUE (fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON,fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON,geometryOfCellRepresentativeToTreatment,geometryOfCellRepresentativeToBuffer,fk_site_selectedSiteID),
* */
)
create table if not exists OpDependentParticular(
AoCForCellsRepresentativeToTreatment float8,
AoCForCellsRepresentativeToBuffer float8,
AvgHPerWindowRepresentativeToTreatment float8,
AvgHPerWindowRepresentativeToBuffer float8,
primary key(AoCForCellsRepresentativeToTreatment,AoCForCellsRepresentativeToBuffer,AvgHPerWindowRepresentativeToTreatment,
AvgHPerWindowRepresentativeToBuffer)
)
create table if not exists GridCellOpDependentParticular(
fk_gridCell_fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON jsonb,
fk_gridCell_fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON jsonb,
fk_gridCell_geometryOfCellRepresentativeToTreatment geometry,
fk_gridCell_geometryOfCellRepresentativeToBuffer geometry,
fk_gridCell_fk_site_selectedSiteID text,
fk_gridCell_fk_OpIndependentParticular_isTreatment boolean,
fk_gridCell_fk_OpIndependentParticular_isBuffer boolean,
fk_gridCell_fk_OpIndependentParticular_distanceFromCPOfTreatmentToNearestEdge float8,
fk_gridCell_fk_OpIndependentParticular_distanceFromCPOfBufferToNearestEdge float8,
foreign key (fk_gridCell_fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON,fk_gridCell_fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON,
fk_gridCell_geometryOfCellRepresentativeToTreatment,fk_gridCell_geometryOfCellRepresentativeToBuffer,fk_gridCell_fk_site_selectedSiteID,
fk_gridCell_fk_OpIndependentParticular_isTreatment,fk_gridCell_fk_OpIndependentParticular_isBuffer,fk_gridCell_fk_OpIndependentParticular_distanceFromCPOfTreatmentToNearestEdge,
fk_gridCell_fk_OpIndependentParticular_distanceFromCPOfBufferToNearestEdge) references GridCell(fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON,
fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON,geometryOfCellRepresentativeToTreatment,geometryOfCellRepresentativeToBuffer,fk_site_selectedSiteID,fk_OpIndependentParticular_isTreatment,
fk_OpIndependentParticular_isBuffer,fk_OpIndependentParticular_distanceFromCPOfTreatmentToNearestEdge,fk_OpIndependentParticular_distanceFromCPOfBufferToNearestEdge),
fk_OpDependentParticular_AoCForCellsRepresentativeToTreatment float8,
fk_OpDependentParticular_AoCForCellsRepresentativeToBuffer float8,
fk_OpDependentParticular_AvgHPerWindowRepresentativeToTreatment float8,
fk_OpDependentParticular_AvgHPerWindowRepresentativeToBuffer float8,
foreign key (fk_OpDependentParticular_AoCForCellsRepresentativeToTreatment,fk_OpDependentParticular_AoCForCellsRepresentativeToBuffer,
fk_OpDependentParticular_AvgHPerWindowRepresentativeToTreatment,fk_OpDependentParticular_AvgHPerWindowRepresentativeToBuffer) references OpDependentParticular(
AoCForCellsRepresentativeToTreatment,AoCForCellsRepresentativeToBuffer,AvgHPerWindowRepresentativeToTreatment,AvgHPerWindowRepresentativeToBuffer),
primary key (fk_gridCell_fourCornersOfKeyWindowRepresentativeToTreatmentAsGeoJSON,fk_gridCell_fourCornersOfKeyWindowRepresentativeToBufferAsGeoJSON,
fk_gridCell_geometryOfCellRepresentativeToTreatment,fk_gridCell_geometryOfCellRepresentativeToBuffer,fk_gridCell_fk_site_selectedSiteID,fk_gridCell_fk_OpIndependentParticular_isTreatment,
fk_gridCell_fk_OpIndependentParticular_isBuffer,fk_gridCell_fk_OpIndependentParticular_distanceFromCPOfTreatmentToNearestEdge,
fk_gridCell_fk_OpIndependentParticular_distanceFromCPOfBufferToNearestEdge,fk_OpDependentParticular_AoCForCellsRepresentativeToTreatment,fk_OpDependentParticular_AoCForCellsRepresentativeToBuffer,
fk_OpDependentParticular_AvgHPerWindowRepresentativeToTreatment,fk_OpDependentParticular_AvgHPerWindowRepresentativeToBuffer)
);
【问题讨论】:
标签: postgresql foreign-keys postgis composite-primary-key