昔neo4jをRailsで使うときにmigrationが必要だった覚えがなかったのだが、いつの間にか必要になったのかもしれない。1
環境
- Rails
5.1.2.rc1
- Neo4j
3.2.1
- neo4jrb
8.1.2
- zsh
5.2(x86_64-apple-darwin16.0)
Migrationをしないとエラーがでる
たぶん、モデルを手動で app/models
以下に作って起動すると下記のようなエラーがでると思う。
❯ bin/rails c
Running via Spring preloader in process 5942
Loading development environment (Rails 5.1.2.rc1)
irb(main):001:0> Writer.count
ETHON: Libcurl initialized
ETHON: performed EASY effective_url=http://localhost:7474/db/data/schema/constraint response_code=200 return_code=ok total_time=0.007093
HTTP REQUEST: 14ms GET http://localhost:7474/db/data/schema/constraint (0 bytes)
ETHON: performed EASY effective_url=http://localhost:7474/db/data/schema/index response_code=200 return_code=ok total_time=0.001799
HTTP REQUEST: 3ms GET http://localhost:7474/db/data/schema/index (0 bytes)
Neo4j::DeprecatedSchemaDefinitionError: Some schema elements were defined by the model (which is no longer supported), but they do not exist in the database. Run the following to create them if you haven't already:
rake neo4j:generate_schema_migration[constraint,Writer,uuid]
rake neo4j:generate_schema_migration[index,Writer,name]
And then run `rake neo4j:migrate`
(zshell users may need to escape the brackets)
from (irb):1
irb(main):002:0>
なるほどなるほど。マイグレーションすればいいのね!
意気揚々と
> bin/rails neo4j:generate_schema_migration[constraint,Writer,uuid]
を叩くと、
> bin/rails neo4j:generate_schema_migration[constraint,Writer,uuid]
zsh: no matches found: neo4j:generate_schema_migration[constraint,Writer,uuid]
え? zshがエラー吐いてるぞ??(重要)
ここで、まだ原因を把握してなかったので、neo4jのmigrationのドキュメントをあさっていました。
いや〜、やっぱりv8で仕様がちょっと変わったっぽいけど、やっぱりmigrationは必要そうだな〜。
と、30分ぐらい悩んでいたのですが、もう1度コマンドを叩くと原因が分かりました。
ちゃんと説明がありましたね!
(zshell users may need to escape the brackets)
意訳(zshユーザーはブラケットのエスケープが必要かもしれない)
それやん!
解決策
bin/rails neo4j:generate_schema_migration\[constraint,Writer,uuid\]
これで通りました。
(bashに変更して該当コマンドを叩くのも成功しました)
余談
migrationの中身
uuidのUnique制約を付けているようです。
class ForceCreateWriterUuidConstraint < Neo4j::Migrations::Base
def up
add_constraint :Writer, :uuid, force: true
end
def down
drop_constraint :Writer, :uuid
end
end
記事を書いてから、全く同じことでハマっている人がいたことに気づいてしまった・・・
-
昔アルバイトでコードを書いてるときに、railsとneo4jを使っていたのだが、ひょっとしたら先輩社員が全部migrationを済ませてくれてたから、必要がなかっただけかもしれない。今回初めてセットアップから、新規プロジェクトとしてやるので、必要になってるだけかも。 ↩