我可以通过 Rails 迁移删除或更改 PostgreSQL 表的几何维度约束吗?
问题
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不熟悉空间适配器字段/选项,因此这段代码可能有点不对劲,但是像这样的东西怎么样?
I'm not familiar with the spatial adapter fields/options so this code might be a little off, but how about something like this?
如果不通过 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).
一些原始 SQL 语句最终出现在数据库模式中。以下将列类型从几何修改为地理:
迁移正确修改了
db/schema.rb
:(在 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:
The migration modifies
db/schema.rb
correctly :(Tested under Rails 3.2.13 and activerecord-postgis-adapter 0.6.6)