0

I have a table with name buildings and I want to drop it because i forgot a column. In cmd I go to the folder and type "sqlite3" then ".tables" and nothing shows me. why that? and what can i do?

marios
  • 261
  • 8
  • 26

2 Answers2

1

Proper way to drop database table is to write migration for it.

Migrations are a convenient way for you to alter your database in a structured and organized manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run them. You’d also have to keep track of which changes need to be run against the production machines next time you deploy.

Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema.rb file to match the structure of your database.

Source: http://guides.rubyonrails.org/migrations.html

Иван Бишевац
  • 13,811
  • 21
  • 66
  • 93
  • i put " def down drop_table :buildings end" and then i go to cmd rake db:migrate and nothing change – marios Feb 13 '13 at 16:39
0

You should avoid directly manipulating the database and use migrations instead. If you forgot to add a column then you should create a new migration using add_column to add the column to the table.

Alternativly if the previous migration was the one that created the table with the missing column and you've not distributed this migration yet you can do rake db:rollback, editing the migration and run rake db:migrate again.

Your lack of tables when running the sqlite command is most likely because you forgot to specify the sqlite file to load: sqlite3 <db_name>

Tomdarkness
  • 3,800
  • 2
  • 21
  • 26
  • i cant do db:rollback because is not the previous migration. i try to do sqlite3 buildings but nothing change when i do .tables – marios Feb 13 '13 at 16:35
  • If it is not the previous migration then write another, new, migration to add the column to the table. Use `add_column`. – Tomdarkness Feb 13 '13 at 16:38
  • can you give a an example please? (what to type in cmd) – marios Feb 13 '13 at 16:41
  • See: http://guides.rubyonrails.org/migrations.html all you need to do is create a migration: `rails g migration ` and add `add_column :, :, :` in the change method. This will create a migration that will add your missing column to the table. You can then run it using `rake db:migrate`. There is no need to drop the whole table because you are missing a single column. – Tomdarkness Feb 13 '13 at 16:44
  • c:\Sites\projectproperties>rails g migration CreateBuildings location:string invoke active_record Another migration is already named create_buildings: c:/Sites/projectproperties/ db/migrate/20120723200049_create_buildings.rb – marios Feb 13 '13 at 16:51
  • You can't have two migrations with the same name, call the migration something else other than `CreateBuildings` – Tomdarkness Feb 13 '13 at 16:52
  • You should not do it manually, you should write a migration for it else you are going to run into even more problems. I suggest you read the guide: http://guides.rubyonrails.org/migrations.html but as I said before all you need it a migration with the add_column with the syntax I provided. This will work, either you are not following my instructions are you are doing something else wrong. – Tomdarkness Feb 13 '13 at 17:47