【发布时间】:2017-09-04 17:28:06
【问题描述】:
我正在用 ruby 创建一个独立的项目。我想进行与 rails 相同的迁移,所以我安装了 gem standalone-migrations。我使用这个 config.yml development:
adapter: postgresql
database: gametour
encoding: utf8
host: localhost
username: tylo
password: ~
然后是我如何创建我的第一个迁移:
rake db:new_migration name=flower_type_migration
class FlowerTypeMigration < ActiveRecord::Migration
def change
create_table :flowerTypes do |t|
t.string :type
end
end
end
rake db:migrate
似乎 rake db:migrate 工作正常,因为我可以在 psql 中看到数据库和表。
但是当我尝试在 ruby 中创建记录时:
FlowerTypes.create(:type => "test"}
我得到这个错误:
/var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:425:in 'clear_transaction_record_state': undefined method '[]' for nil:NilClass (NoMethodError)
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:339:in 'ensure in rollback_active_record_state!'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:339:in 'rollback_active_record_state!'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/transactions.rb:318:in 'save'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/suppressor.rb:41:in 'save'
from /var/lib/gems/2.3.0/gems/activerecord-5.0.2/lib/active_record/persistence.rb:34:in 'create'
我已尝试向我自己 (tylo) 和 PUBLIC 授予权限,但仍然遇到相同的错误。
我一定是错过了什么,有什么解决办法吗?
【问题讨论】:
-
如果您
SELECT * FROM "flowerTypes";会发生什么?不带引号的标识符在 PostgreSQL 中被折叠成小写,所以如果你创建一个混合大小写的表名,那么你必须在任何地方引用它。 -
它适用于引用是的哈哈。我认为这个问题与红宝石有关,但似乎并非如此,您对创建不起作用有任何想法吗?谢谢顺便说一句我不知道。
-
(1) PostgreSQL 的最佳实践是创建小写的表名(和其他标识符),并用下划线分隔单词,这样您就不必担心引用。 (2) 我在该表中没有看到
name列,但您正在尝试使用name创建记录。 -
是的,这是有道理的。对不起,我是手工写的,我在那里犯了一个错误,但我在我的代码中写了 :type。
标签: ruby postgresql activerecord database-migration