我可以通过 Rails 迁移删除或更改 PostgreSQL 表的几何维度约束吗?

发布于 2024-10-13 02:02:56 字数 808 浏览 1 评论 0原文

问题

我正在使用 Rails 2.3 和 Spatial Adapter 插件 和带有 PostGIS 扩展的 Postgres 8.4 数据库。

不久前,通过如下迁移将几何字段添加到表中:

add_column :my_table, :the_geom, :geometry, :srid => 4326

与字段一起创建表维度约束。在这种情况下,它确保输入到 the_geom 字段的几何图形确实具有两个维度

现在,我想添加二维以上的几何图形。 我可以使用什么 Rails 迁移代码来更改约束以允许两个以上的维度或完全删除它?

什么不起作用?

当我通过 add_column 创建新的几何字段时, code> 方法,我知道我可以使用 :with_z:with_m 选项来增加允许的维度数(使用这两个选项时,约束将允许最多四个维度).
但是,当将这些选项与 change_column 命令一起使用时,它似乎对约束没有任何影响。
我不想删除该列并重新添加它,因为那样我就会丢失所有数据。

我知道,我可以使用原始 SQL 来删除约束(我也可以使用迁移或 rake 任务作为包装器)。这样做的问题是,它对测试数据库没有影响,因为当我运行所有测试时它总是从头开始创建。

The question

I am using Rails 2.3 with the Spatial Adapter plugin and a Postgres 8.4 database with PostGIS extension.

A while ago, a geometry field has been added to a table via a migration like this:

add_column :my_table, :the_geom, :geometry, :srid => 4326

Along with the field, a table dimension constraint is created. In this case it makes sure the geometry entered into the the_geom field does have exactly two dimensions.

Now, I'd like to add geometries with more than 2 dimensions. What Rails migration code could I use to either change the constraint to allow more than two dimensions or remove it altogether?

What didn't work

When I create a new geometry field via the add_column method, I know I can use the :with_z and :with_m options to increase the number of dimensions allowed (the constraint would allow up to four dimension when using both options).
However, when using these options with the change_column command, it doesn't seem to have any effect on the constraint.
I don't want to remove the column and re-add it, because then I lose all data.

I know, I can use raw SQL to remove the constraint (I could also use a migration or rake task as a wrapper). The problem with this is, that it doesn't have an effect on the test database, because it is always created from scratch when I run all tests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绅士风度i 2024-10-20 02:02:56

我不熟悉空间适配器字段/选项,因此这段代码可能有点不对劲,但是像这样的东西怎么样?

class UpgradeGeometryMigration < ActiveRecord::Migration
  def self.up
    rename_column :my_table, :the_geom, :the_geom_old
    add_column    :my_table, :the_geom, :geometry, :srid => 4326, :with_z => true, :with_m => true

    # Add some code here to iterate through all of your records in the 
    # :the_geom_old table and copy them into the new :the_geom field

    remove_column :my_table, :the_geom_old

    # Add any indices required for :the_geom
  end
end

I'm not familiar with the spatial adapter fields/options so this code might be a little off, but how about something like this?

class UpgradeGeometryMigration < ActiveRecord::Migration
  def self.up
    rename_column :my_table, :the_geom, :the_geom_old
    add_column    :my_table, :the_geom, :geometry, :srid => 4326, :with_z => true, :with_m => true

    # Add some code here to iterate through all of your records in the 
    # :the_geom_old table and copy them into the new :the_geom field

    remove_column :my_table, :the_geom_old

    # Add any indices required for :the_geom
  end
end
云雾 2024-10-20 02:02:56

如果不通过 ActiveRecord::Base.connection.execute 转向原始 SQL,就无法更改几何列。

不幸的是,原始 SQL 语句不会进入 Rails 数据库模式,因此在重新创建数据库时(例如用于测试),约束仍保留在数据库中。

There is no way to alter a geometry column without turning to raw SQL via ActiveRecord::Base.connection.execute.

Unfortunately raw SQL statements do not make it into the Rails database schema and so the constraints remain in the DB when it is recreated (for tests for example).

凉墨 2024-10-20 02:02:56

一些原始 SQL 语句最终出现在数据库模式中。以下将列类型从几何修改为地理:

class ChangePostGisGeometryToGeography < ActiveRecord::Migration
  def up
    execute <<-SQL
      ALTER TABLE mytable ALTER COLUMN geom TYPE geography(MULTIPOLYGON,4326);
    SQL
  end
end

迁移正确修改了 db/schema.rb

-    t.spatial  "geom", :limit => {:srid=>4326, :type=>"multi_polygon"}
+    t.spatial  "geom", :limit => {:srid=>4326, :type=>"multi_polygon", :geographic=>true}

(在 Rails 3.2.13 和 activerecord-postgis-adapter 0.6.6 下测试)

Some raw SQL statements end up in the database schema. The following modifies the column type from geometry to geography:

class ChangePostGisGeometryToGeography < ActiveRecord::Migration
  def up
    execute <<-SQL
      ALTER TABLE mytable ALTER COLUMN geom TYPE geography(MULTIPOLYGON,4326);
    SQL
  end
end

The migration modifies db/schema.rb correctly :

-    t.spatial  "geom", :limit => {:srid=>4326, :type=>"multi_polygon"}
+    t.spatial  "geom", :limit => {:srid=>4326, :type=>"multi_polygon", :geographic=>true}

(Tested under Rails 3.2.13 and activerecord-postgis-adapter 0.6.6)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文