DEV Community

Create table in Rails only if it does not exist already

Prathamesh Sonpatki on January 17, 2020

In Rails, we create the tables using the migrations. A typical create table migration looks like this. create_table :repo_subscriptions do |t| ...
Collapse
 
andrewbrown profile image
Andrew Brown πŸ‡¨πŸ‡¦

You can also force a table to be dropped and then created.
A bit more destructive but works well.

create_table :repo_subscriptions, force: true do |t|
Enter fullscreen mode Exit fullscreen mode
Collapse
 
prathamesh profile image
Prathamesh Sonpatki

Yup it’s useful for tests but a bit destructive for production πŸ˜„

Collapse
 
arnabsen1729 profile image
Arnab Sen

I was actually looking for this. Thanks

Collapse
 
cescquintero profile image
Francisco Quintero πŸ‡¨πŸ‡΄

This is so awesome. Thanks for sharing. Rails full of things like this we cannot find in the docs regularly or easily.