How to automatically generate ER diagram when migrating with Rails6


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.

Development environment

Rails: 6.0.3 Ruby:2.7.1 Macbook Pro Graphviz GraphQL (This was due to the automatically generated file.)

ER diagram settings

Install Gem.


group :development do
  gem 'rails-erd'

Set to gitignore if necessary.



You can generate an ER diagram with the following command.

$ bundle exec erd

Auto-generated settings

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.


#Hook the task of migrate
Rake::Task['db:migrate'].enhance do
  if Rails.env.development?

#Tasks after migrate
task after_migrate: :environment do

#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'

Error countermeasures

Not recommended measures

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.autoloader = :classic

About zeitwerk uninitialized constant XXX (NameError) error [Rails Guide 2 Enable Zeitwerk Mode]( E6% 9C% 89% E5% 8A% B9% E3% 81% AB% E3% 81% 99% E3% 82% 8B) [When Rails Guide 11 Zeitwerk is not used]( % 81% 84% E5% A0% B4% E5% 90% 88)

Measures taken

When I generated the ER diagram, I got the following error.


$ 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.


module Types
  module BaseInterface
    include GraphQL::Schema::Interface
    field_class Types::BaseField 

After measures ↓


module Types::BaseInterface
  include GraphQL::Schema::Interface
  field_class Types::BaseField

Change other files as well.


module Types
  class BaseScalar < GraphQL::Schema::Scalar

After measures ↓


class Types::BaseScalar < GraphQL::Schema::Scalar

When I generated the ER diagram again, the following error appeared this time.


$ 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!

At the end

Since Rails6 x GraphQL was used, the auroload could not be performed correctly, and the error occurred.

Recommended Posts

How to automatically generate ER diagram when migrating with Rails6
How to manually generate a JWT with Rails Knock
How to set environment variables when using Payjp with Rails
How to specify db when creating an app with rails
How to resolve errors when installing Rails 5.1.3
How to build Rails 6 environment with Docker
[Rails] How to write when making a subquery
How to build API with GraphQL and Rails
[Rails] How to build an environment with Docker
How to automatically generate a constructor in Eclipse
How to write Rails
How to uninstall Rails
How to push an app developed with Rails to Github
How to delete a new_record object built with Rails
Automatically deploy to WildFly with Jenkins when SVN commits
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
How to query Array in jsonb with Rails + postgres
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
[rails] How to post images
[Rails] How to use enum
[Rails] How to use enum
How to read rails routes
[Easy] How to automatically format Ruby erb file with vsCode
[With back tricks] How to introduce React to the simplest Rails
How to use rails join
How to number (number) with html.erb
How to update with activerecord-import
How to write and notes when migrating from VB to JAVA
How to terminate rails server
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Rails / RSpec] How to deal with element has zero size error
Is it possible to automatically generate Getters / Setters with Java Interface?
[Rails] How to introduce kaminari with Slim and change the design
How to install Pry after building Rails development environment with Docker
[Rails] How to avoid "Use hash rockets syntax" when executing Rubocop
What I was addicted to when implementing google authentication with rails
How to get boolean value with jQuery in rails simple form
How to cure screen flickering when fading pages with CSS animations
How to rename a model with foreign key constraints in Rails
How to build Rails + Vue + MySQL environment with Docker [2020/09 latest version]
How to write when installing Amazon Corretto 8 on CentOS 8 with Ansible.
[Rails] I want to add data to Params when transitioning with link_to
[Rails 5] How to display the password change screen when using devise
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to get started with slim
[rails] How to display db information
[Rails] How to prevent screen transition
How to enclose any character with "~"
How to use Ruby on Rails