How to automatically generate ER diagram when migrating with Rails6

Introduction

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.

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

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.

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

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/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)

Measures taken

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!

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