SQL ActiveRecord:从控制台列出表中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5575970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:03:17  来源:igfitidea点击:

ActiveRecord: List columns in table from console

sqlruby-on-railsruby-on-rails-3activerecord

提问by Andrew

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

我知道您可以要求 ActiveRecord 使用以下命令在控制台中列出表:

ActiveRecord::Base.connection.tables

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

是否有一个命令可以列出给定表中的列?

回答by Pravin

This will list the column_names from a table

这将列出表中的 column_names

Model.column_names
e.g. User.column_names

回答by Aaron Henderson

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,因此不需要模型。方便快速输出数据库的结构。

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

示例输出:http: //screencast.com/t/EsNlvJEqM

回答by Yule

Using rails three you can just type the model name:

使用 rails 3,您只需键入型号名称:

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

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

在rails 4中,需要先建立连接:

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...)

回答by gm2008

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.

如果您对 SQL 命令感到满意,则可以进入应用程序的文件夹并运行rails db,这是rails dbconsole. 它会进入你的数据库的shell,无论是sqlite还是mysql。

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

然后,您可以使用 sql 命令查询表列,例如:

pragma table_info(your_table);

回答by codeepic

You can run rails dbconsolein you command line tool to open sqlite console. Then type in .tablesto list all the tables and .fullschemato get a list of all tables with column names and types.

您可以rails dbconsole在命令行工具中运行以打开 sqlite 控制台。然后键入.tables以列出所有表并.fullschema获取所有带有列名和类型的表的列表。

回答by Gamaliel

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

补充这些有用的信息,例如使用 rails console o rails dbconsole:

Student is my Model, using rails console:

学生是我的模型,使用 rails 控制台:

$ 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 使用 SQLite 的其他选项:

$ 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!

希望这可以帮助!

回答by csebryam

  • To list the columns in a table I usually go with this:
    Model.column_names.sort.
    i.e. Orders.column_names.sort

    Sorting the column names makes it easy to find what you are looking for.

  • For more information on each of the columns use this:
    Model.columns.map{|column| [column.name, column.sql_type]}.to_h.

  • 要列出一个表,我通常去与此列:
    Model.column_names.sort
    i.e. Orders.column_names.sort

    对列名进行排序可以轻松找到您要查找的内容。

  • 有关每一列的更多信息,请使用:
    Model.columns.map{|column| [column.name, column.sql_type]}.to_h

This will provide a nice hash. for example:

这将提供一个很好的散列。例如:

{
   id => int(4),
   created_at => datetime
}

回答by valk

For a more compact format, and less typing just:

对于更紧凑的格式,更少的输入:

Portfolio.column_types