ActiveRecord: List columns in table from console

94,800

Solution 1

This will list the column_names from a table

Model.column_names
e.g. User.column_names

Solution 2

This gets the columns, not just the column names and uses ActiveRecord::Base::Connection, so no models are necessary. Handy for quickly outputting the structure of a db.

ActiveRecord::Base.connection.tables.each do |table_name|
  puts table_name
  ActiveRecord::Base.connection.columns(table_name).each do |c| 
    puts "- #{c.name}: #{c.type} #{c.limit}"
  end
end

Sample output: http://screencast.com/t/EsNlvJEqM

Solution 3

Using rails three you can just type the model name:

> User
gives:
User(id: integer, name: string, email: string, etc...)

In rails four, you need to establish a connection first:

irb(main):001:0> User
=> User (call 'User.connection' to establish a connection)
irb(main):002:0> User.connection; nil #call nil to stop repl spitting out the connection object (long)
=> nil
irb(main):003:0> User
User(id: integer, name: string, email: string, etc...)

Solution 4

If you are comfortable with SQL commands, you can enter your app's folder and run rails db, which is a brief form of rails dbconsole. It will enter the shell of your database, whether it is sqlite or mysql.

Then, you can query the table columns using sql command like:

pragma table_info(your_table);

Solution 5

complementing this useful information, for example using rails console o rails dbconsole:

Student is my Model, using rails console:

$ rails console
> Student.column_names
 => ["id", "name", "surname", "created_at", "updated_at"] 

> Student
 => Student(id: integer, name: string, surname: string, created_at: datetime, updated_at: datetime)

Other option using SQLite through Rails:

$ rails dbconsole

sqlite> .help

sqlite> .table
ar_internal_metadata  relatives             schools             
relationships         schema_migrations     students 

sqlite> .schema students
CREATE TABLE "students" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "surname" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);

Finally for more information.

sqlite> .help

Hope this helps!

Share:
94,800
Andrew
Author by

Andrew

Polyglot engineer and people leader.

Updated on August 20, 2021

Comments

  • Andrew
    Andrew over 2 years

    I know that you can ask ActiveRecord to list tables in console using:

    ActiveRecord::Base.connection.tables
    

    Is there a command that would list the columns in a given table?

  • owl
    owl about 13 years
    OP just wants the column names.
  • Yule
    Yule about 13 years
    Perhaps. But not necessarily. It is an alternative way of getting them with extra info that is sometimes helpful when listing columns from console
  • Andrew
    Andrew about 13 years
    This is also a useful method to know, IMO. @Yule - does this query the schema/migrations code etc. or does it query the DB? The reason I ask is I was experiencing a mismatch between my schema and what actually was in the DB (one migration glitched), so specifically I needed to be sure I was seeing what was actually in the table.
  • srt32
    srt32 about 10 years
    You could also run something like Model.columns to get more info about the columns including database config data.
  • sayap
    sayap about 10 years
    In rails 3.2, doing it this way somehow doesn't set the primary attribute correctly (all columns have primary=nil). It is set correctly with the Model.columns method suggested by srt32.
  • Yule
    Yule over 9 years
    @Andrew it queries the DB (hence the need to establish a connection in rails 4)
  • nibbex
    nibbex about 9 years
    Great! Using Model.columns provides all the information for a table through ActiveRecord. Crucially for me it was the only and easiest way to gain confidence in what my primary key really was at the database level.
  • AJFaraday
    AJFaraday over 8 years
    You could always use Model.primary_key, which gives you the name of the primary key according to rails. (This will be 'id' unless it's declared in the model as something else).
  • valk
    valk about 7 years
    For mySQL use describe your_table;, not perfect but works
  • oklas
    oklas about 7 years
    You can use online database console (gem activeadmin-sqlpage) as describerd in this answer if you use active admin.
  • Pak
    Pak over 4 years
    Does not exist in rails 5+
  • baash05
    baash05 about 4 years
    This is the right answer. There is no requirement to have a model. Not every table has a model. "has_many_and_belongs_to"
  • Richard_G
    Richard_G about 3 years
    To provide a list sorted by table name, use: ActiveRecord::Base.connection.tables.sort.each
  • A moskal escaping from Russia
    A moskal escaping from Russia about 3 years
    The answer doesn't state that, and the question is tagged Rails 3 @Pak...
  • Pak
    Pak about 3 years
    @SebastianPalma I know, but as this is a well-referenced question such comments are imo helpful for people looking for answers, fast. The tag was probably chosen during the initial write-up but the question is quite generic.