简体   繁体   中英

Creating tables in Postgres and Rails 4

I am a completely new at this; I used the psql console to create tables for the db, but I think I'm doing it wrong since when I run rake db:migrate , everything gets wiped. So my question is how do I get it to stop dropping all my tables upon a reset without typing all my tables again? I think I'm supposed to use models, is this correct?

EDIT - So I have wasted all my time making tables using the psql console?

You need to create tables "the Rails way" with migrations . You should read the link provided, but essentially you create migrations to track the changes in your database schema over time. You have migrations for creating tables, adding new columns, changing column definitions, etc. You can generate a migration to create a table from the command line like so:

rails generate model Fruit title:string amount:integer
rake db:migrate

This will not only create the table fruits with the specified name, it will also create a model Fruit with which you can query the table. You're also free to edit the migration created before running rake db:migrate .

You should create migration files to build your database. That is what you are doing when you run rake db:migrate : you are taking your migration files and applying them to your database. Look at this guide for more information about Rails migrations.

By running $ rake -T you can check every rake task. Everything is being wiped because other tasks are run along with certain rake tasks. Try

$ rake db:drop
$ rake db:create
$ rake db:migrate

Your database should be created properly, as long as you gave database creation permissions to your Postgres role.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM