带有 pg-mem 的 Postgis 地理列给出错误

问题描述 投票:0回答:1

我的 typeORM 实体中有此列:

  @Index({ spatial: true })
  @Column({
    type: 'geography',
    spatialFeatureType: 'Point',
    srid: 4326,
  })
  geolocation!: Point;

我正在使用 pg-mem 进行测试

但是在初始化时它会因此错误而崩溃

    1  CREATE TABLE "user" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(),  "geolocation" geography(Point,4326), ...(other columns)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    Unexpected word token: "point". Instead, I was expecting to see one of the following:

        - A "int" token

所以基本上你可以注册等效类型,但到目前为止我还无法使其工作。

我不确定你是否应该在 postgis 中注册类型或直接在公共场合注册类型,所以我都尝试了。

太胖了,这些都不起作用。

db.registerExtension('postgis', schema => {
  schema.registerEquivalentType({
    name: 'geography',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });

  schema.registerEquivalentType({
    name: 'geography(Point,4326)',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });

  schema.registerEquivalentType({
    name: 'point',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });

  schema.registerEquivalentType({
    name: 'Point',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });

  schema.registerEquivalentType({
    name: 'Point,4326',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });

  schema.registerEquivalentType({
    name: '(Point, 4326)',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });
});

db.public.registerEquivalentType({
  name: 'geography',
  equivalentTo: DataType.point,
  isValid: () => true,
});

db.public.registerEquivalentType({
  name: 'geography(Point,4326)',
  equivalentTo: DataType.point,
  isValid: () => true,
});

db.public.registerEquivalentType({
  name: 'point',
  equivalentTo: DataType.point,
  isValid: geo => geo !== null && geo !== undefined,
});

db.public.registerEquivalentType({
  name: 'Point',
  equivalentTo: DataType.point,
  isValid: geo => geo !== null && geo !== undefined,
});

db.public.registerEquivalentType({
  name: 'Point,4326',
  equivalentTo: DataType.point,
  isValid: geo => geo !== null && geo !== undefined,
});

db.public.registerEquivalentType({
  name: '(Point, 4326)',
  equivalentTo: DataType.point,
  isValid: geo => geo !== null && geo !== undefined,
});


有人能做到这一点吗?它基本上毁了我所有的测试

nestjs typeorm pg-mem
1个回答
0
投票

我认为您的解决方案涉及在

pg-mem
中注册 geography 和 geography(Point,4326) 类型,以将其识别为与点数据类型等效。这确保 pg-mem 可以正确处理这些空间类型,允许创建带有
geography(Point,4326)
列的表而不会出现错误,重点关注基本类型注册。

import { newDb } from 'pg-mem';
import { DataType } from 'pg-mem';

const db = newDb();

db.registerExtension('postgis', schema => {
  schema.registerEquivalentType({
    name: 'geography',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });

  schema.registerEquivalentType({
    name: 'geography(Point,4326)',
    equivalentTo: DataType.point,
    isValid: geo => geo !== null && geo !== undefined,
  });
});

// Add this if needed in the public schema as well
db.public.registerEquivalentType({
  name: 'geography',
  equivalentTo: DataType.point,
  isValid: geo => geo !== null && geo !== undefined,
});

db.public.registerEquivalentType({
  name: 'geography(Point,4326)',
  equivalentTo: DataType.point,
  isValid: geo => geo !== null && geo !== undefined,
});

// Now create your table
db.public.none(`
  CREATE TABLE "user" (
    "id" uuid NOT NULL DEFAULT uuid_generate_v4(),
    "geolocation" geography(Point,4326)
    -- other columns...
  );
`);

console.log("Table created successfully!");
© www.soinside.com 2019 - 2024. All rights reserved.