Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用ActiveRecord在sqlite/pg数据库中存储数组_Ruby On Rails_Sqlite_Postgresql_Activerecord - Fatal编程技术网

Ruby on rails 使用ActiveRecord在sqlite/pg数据库中存储数组

Ruby on rails 使用ActiveRecord在sqlite/pg数据库中存储数组,ruby-on-rails,sqlite,postgresql,activerecord,Ruby On Rails,Sqlite,Postgresql,Activerecord,我正在尝试在heroku PG数据库中存储字符串数组 我尝试将封送转储到在sqlite中工作的字符串。但是,对于PG,如果数组太大,它不会保存整个转储字符串,并且在尝试访问它时无法加载数组。尝试访问数组时,出现错误“封送数据太短” 迁移 add_column :orders, :photo_urls, :text, :limit => nil add_column :orders, :photo_urls, :text, array: true, default: [] 或者,我尝试在

我正在尝试在heroku PG数据库中存储字符串数组

我尝试将封送转储到在sqlite中工作的字符串。但是,对于PG,如果数组太大,它不会保存整个转储字符串,并且在尝试访问它时无法加载数组。尝试访问数组时,出现错误“封送数据太短”

迁移

add_column :orders, :photo_urls, :text, :limit => nil
add_column :orders, :photo_urls, :text, array: true, default: []
或者,我尝试在模型中使用
serialize:photo\u url
int。同样,这在sqlite中有效,但在生产PG中,我得到错误
数组值必须以“{”或维度信息开头

使用不同的默认设置尝试了两次迁移:

add_column :orders, :photo_urls, :text, array: true, default: []
add_column :orders, :photo_urls, :text, array: true, default: '{}'

在sqlite和PG中存储数组有不同的方法吗?

在我的开发环境中,我只能为sqlite序列化数组

serialize :photo_urls if Rails.env.development?
迁移

add_column :orders, :photo_urls, :text, :limit => nil
add_column :orders, :photo_urls, :text, array: true, default: []

但是你不能以同样的方式查询列。如果你这样做是为了在SQLite上开发并在PostgreSQL上部署,那么你犯了一个很大的错误。你能给我举个例子吗?除了调用
order.photo_URL
,我不会使用任何涉及arrayPostgreSQL的查询,你不能对序列化列执行任何这类操作;如果您不需要在数组内部进行查询,那么这就无关紧要了。两个数据库之间的类型工作方式、字符串长度处理、分组方式等方面的差异迟早会导致问题。它只是在我的开发环境中进行序列化,该环境使用sqlite。但我理解您的说法,它可能会引入意外的comp应用程序。您对替代实现有何建议?请在您的开发环境中安装PostgreSQL。不要指望ActiveRecord在可移植性方面给您带来太多帮助。