Search code examples
ruby-on-railsrubyorm

Cannot create database by using rails migration


I have just faced to problem about using rails db:migrate. The below images show exactly what problem I have faced. [This is the error when I run command rails db:migrate](https://i.sstatic.net/Q696O.png)

And this is my code:

class CreateAdmins < ActiveRecord::Migration[7.0]
  def change
    create_table :admins do |t|

      t.string :email, limit: 50
      t.string :encrypted_password, limit: 100
      t.integer :sign_in_count, default: 0
      t.string :hashid1, limit: 100
      t.string :hashid2, limit: 100
      t.string :encrypted_fullname_vi, limit: 100
      t.string :encrypted_fullname_en, limit: 100
      t.string :encrypted_birthday, limit: 100
      t.string :encrypted_address, limit: 100
      t.string :nickname
      t.json :image_json
      t.integer :show_avatar, limit: 1, default: 0

      t.timestamps
    end
  end
end

I tried many ways in this forum but it was still ineffective. How can I fix this?

Here are the details about my environment: Ruby: 3.1.4 Rails: 7.0.8 Migration: 7.0


Solution

  • I think you've created your Rails project with the Postgres flag but then you're using the MySQL2 gem and/or connecting to a MySQL database.

    If you want to solve it, you can either switch to a Postgres database or use the MySQL2 gem and re-run your model scaffolding.

    You can run bin/rails db:drop db:create to restart afresh.