We have summarized how to automatically generate ER diagram in Rails6. In the case of Rails6, Zeitwerk is used by default for autoloading files, but due to this, ER diagram could not be generated well, so the workaround is also described.
Rails: 6.0.3 Ruby:2.7.1 Macbook Pro Graphviz GraphQL (This was due to the automatically generated file.)
Install Gem.
Gemfile
group :development do
gem 'rails-erd'
end
Set to gitignore if necessary.
.gitignore
*.dot
You can generate an ER diagram with the following command.
$ bundle exec erd
To automatically generate ER diagram at the time of migration, set in Rakefile. If you don't need automatic generation, you don't need to mention it here.
Rakefile
#Hook the task of migrate
Rake::Task['db:migrate'].enhance do
if Rails.env.development?
Rake::Task[:after_migrate].invoke
end
end
#Tasks after migrate
task after_migrate: :environment do
Rake::Task[:create_erd].invoke
end
#Create ER diagram
task create_erd: :environment do
# attributes=foreign_keys,primary_keys,timestamps (Attributes display primary key, foreign key, timestamp)
# sort=false (Do not sort column names alphabetically)
# filename=loof-api-server-erd (file name)
# filetype=dot (File extension)
sh 'bundle exec erd --attributes=foreign_keys,primary_keys,content,timestamps --sort=false --filename=hogehoge --filetype=dot'
end
When I stopped using zeitwerk, which autoloads files, and changed it to classic, I was able to automatically generate it, but Rspec did not pass.
config/application.rb
config.autoloader = :classic
About zeitwerk uninitialized constant XXX (NameError) error [Rails Guide 2 Enable Zeitwerk Mode](https://railsguides.jp/autoloading_and_reloading_constants.html#zeitwerk%E3%83%A2%E3%83%BC%E3%83%89%E3%82%92% E6% 9C% 89% E5% 8A% B9% E3% 81% AB% E3% 81% 99% E3% 82% 8B) [When Rails Guide 11 Zeitwerk is not used](https://railsguides.jp/autoloading_and_reloading_constants.html#zeitwerk%E3%82%92%E4%BD%BF%E3%82%8F%E3%81%AA%E3 % 81% 84% E5% A0% B4% E5% 90% 88)
When I generated the ER diagram, I got the following error.
Terminal
$ bundle exec erd
Failed: Zeitwerk::NameError: expected file /app/graphql/interface_types/base_interface.rb to define constant Types::BaseInterface, but didn't
Apparently the module definition method is not appropriate. How to break Zeitwerk
The cause was the file automatically generated by GraphQL, so I changed it as follows.
base_interface.rb
module Types
module BaseInterface
include GraphQL::Schema::Interface
field_class Types::BaseField
end
end
After measures ↓
base_interface.rb
module Types::BaseInterface
include GraphQL::Schema::Interface
field_class Types::BaseField
end
Change other files as well.
base_scalar.rb
module Types
class BaseScalar < GraphQL::Schema::Scalar
end
end
After measures ↓
base_scalar.rb
class Types::BaseScalar < GraphQL::Schema::Scalar
end
When I generated the ER diagram again, the following error appeared this time.
Terminal
$ bundle exec erd
Failed: RuntimeError: Saving diagram failed!
Verify that Graphviz is installed and in your path, or use filetype=dot.
It is necessary to specify file as dot.
Terminal(Specify file)
$ bundle exec erd --filetype=dot
You have successfully generated an ER diagram!
Since Rails6 x GraphQL was used, the auroload could not be performed correctly, and the error occurred.
Recommended Posts