I have a following table and i would like to ask a question regarding coordinates check constraint.
create table places
(
id bigint generated always as identity
constraint pk_places
primary key,
name varchar(128) not null,
address varchar(256) not null,
region_name varchar not null
constraint fk_places_region_name_regions
references regions
on update cascade on delete restrict,
coordinates geography(Point, 4326)
I order to prevent wrong coordinates from being inserted in the table i have made following check constraint:
alter table places
add check (
abs(ST_X(coordinates::geometry)) <= 90 and abs(ST_Y(coordinates::geometry)) <= 180
);
It does work but as for me it seems quite fishy because:
- Well, geography type
POINT
, considering planet Earth should have restriction of lattitude and longitude of 90/180 degr respectively. But it does not. Perhaps it is any way how to switch it on somewhere maybe? - In my check constraint i need to cast geography into geometry. Maybe there is any native function for geography to get lat and lon?
- Is it any better solution to restrict geo coordinates values?
Than you.