[英]How to show active-record tables and their columns on the CLI?
我是主動記錄的新手,正在做表遷移。 我想在終端上查看已遷移的表及其對應的列名。 有人知道執行此操作的命令嗎?
假設我有2張桌子的貓和狗,是否有一個命令可以在CLI中顯示類似的內容?
dogs
---------
name
breed
age
cats
---------
name
breed
age
有幾種方法可以獲取有關數據庫的信息,但這應該可以從rails console
:
ActiveRecord::Base.connection.tables.each do |table|
next unless ActiveRecord.const_defined?(table.classify) && !table.match(/schema_migrations/)
puts table.classify.to_s
puts '-----'
puts table.classify.constantize.column_names
puts
puts
end;
您可能正在尋找:
ActiveRecord::Base.connection.tables
和
ActiveRecord::Base.connection.table_structure("projects")
如果您的目標只是在命令行上查看表結構,則可以打印db/schema.rb
的內容,Rails在運行遷移時將其維護。 該文件包括表名,列名和數據類型。 它甚至包括索引。
在macOS或Linux上: cat db/schema.rb
在Windows上: type db/schema.rb
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.