Andrey
2 min readJul 20, 2021

--

Fixing your Database Structure (SQLite)

We’re used to creating databases with create_table, but we have several methods under change_table at our disposal to create an additional migration if our initial migration does not go as planned. We must run the migration again to make the change.

change_table(:tablename) do |t|
t.remove :data
end
t.column #adds an ordinary column. Ex: t.column(:name, :string)
t.index #adds a new index
t.change #changes the column definition. Ex: t.change(:name, :string, :limit => 80)
t.rename #changes the name of the column.

A full list of methods can be found here: https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table#226-All-methods

Dropping Tables and Deleting Databases

db:drop deletes the table structure from the database, along with any data stored in the tabledb:reset does db:drop, db:setup (db:schema:load recreates database from schema.rb, db:seed db/seeds.rb)db:rollback rolls back the last migration

Deleting the database file. If you need to completely remove a database, you will need to delete the database file from the file system. Since SQLite is file based (not server) it stores its databases as a normal file, which is not affected itself when we remove the database from the current environment

All Rails db Rake Tasks: https://jacopretorius.net/2014/02/all-rails-db-rake-tasks-and-what-they-do.html

What you can’t name tables. Reserved words

While it’s not good practice to name tables, indices, columns, databases or user-defined functions with reserved words, surprisingly enough, creating a table with a reserved keyword does not immediately throw an error. Any of the following words will make it harder to access / edit your tables and should be avoided.

Full list of keywords can be found here: https://www.sqlite.org/lang_keywords.html

--

--