简体   繁体   English

Rails框架创建的数据库表是什么样的?

[英]What does a db table created by the Rails framework look like?

I don't have a Rails environment set up and this is actually quite hard to find a quick answer for, so I'll ask the experts. 我没有设置Rails环境,实际上很难找到一个快速的答案,因此我将请专家解决。

When Rails creates a table based on your "model" that you have set up, does Rails create a table that mirrors this model exactly, or does it add in more fields to the table to help it work its magic? 当Rails根据已设置的“模型”创建表时,Rails是否创建一个表来精确地反映此模型,还是在表中添加了更多字段以帮助其发挥作用? If so, what other fields does it add and why? 如果是这样,它还会添加哪些其他字段?为什么? Perhaps you could cut and paste the table structure, or simply point me to a doc or tutorial section that addresses this. 也许您可以剪切并粘贴表结构,或者直接将我指向解决此问题的文档或教程部分。

If you're building a completely new application, including a new database, then you can build the whole back end with migrations. 如果您要构建一个全新的应用程序,包括一个新的数据库,则可以通过迁移来构建整个后端。 Running 跑步

ruby script/generate model User name:string

produces both a user.rb file for the model and a migration: 生成模型的user.rb文件和迁移文件:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

You can see that by default the generate script adds "timestamps" for (created and last updated) and they're managed automatically if allowed to remain present. 您可以看到,默认情况下,generate脚本会为(创建和上次更新)添加“时间戳”,并且如果允许它们保持存在,则会自动对其进行管理。

Not visible, but important, is that an extra column, "id", is created to be the single primary key. 不可见,但重要的是,创建了一个额外的列“ id”作为单个主键。 It's not complusory, though - you can specify your own primary key in the model, which is useful if you're working with a legacy schema. 但是,它不是多余的-您可以在模型中指定自己的主键,如果您使用的是旧式架构,这将非常有用。 Assuming you retain id as the key, then Rails will use whatever RDBMS-specific features are available for new key values. 假设您保留id作为键,那么Rails将使用可用于新键值的RDBMS特定的任何功能。

In ActiveRecord, models are created from database tables, not the other way around. 在ActiveRecord中,模型是从数据库表创建的,而不是相反的。

You may also want to look into Migrations, which is a way of describing and creating the database from Ruby code. 您可能还需要研究Migrations,这是一种从Ruby代码描述和创建数据库的方法。 However, the migration is not related to the model; 但是,迁移与模型无关。 the model is still created at runtime based on the shape of the database. 仍然会在运行时根据数据库的形状创建模型。

There are screencasts related to ActiveRecord and Migrations on the Rails site: http://www.rubyonrails.org/screencasts Rails站点上有与ActiveRecord和Migrations相关的截屏视频: http : //www.rubyonrails.org/screencasts

Here 's the official documentation for ActiveRecord. 是ActiveRecord的官方文档。 It agrees with Brad. 它同意布拉德。 You might have seen either a different access method or a migration (which alters the tables and thus the model) 您可能已经看到了不同的访问方法或迁移(这会更改表并因此更改模型)

I have had a little experience moving legacy databases into Rails and accessing Rails databases from outside scripts. 我有一些将旧数据库移入Rails并从外部脚本访问Rails数据库的经验。 That sounds like what you're trying to do. 这听起来像您要尝试执行的操作。 My experience is in Rails databases built on top of MySQL, so your mileage may vary. 我的经验是基于MySQL构建的Rails数据库,因此您的工作量可能会有所不同。

The one hidden field is the most obvious --- the "id" field (an integer) that Rails uses as its default primary key. 一个隐藏的字段是最明显的--- Rails用作其默认主键的“ id”字段(整数)。 Unless you specify otherwise, each model in Rails has an "id" field that is a unique, incremented integer primary key. 除非另有说明,否则Rails中的每个模型都有一个“ id”字段,该字段是唯一的,递增的整数主键。 This "id" field will appear automatically in any model generated within Rails through a migration, unless you tell Rails not to do so (by specifying a different field to be the primary key). 除非您告知Rails请勿这样做(通过将其他字段指定为主键),否则该“ id”字段将自动出现在通过迁移在Rails内生成的任何模型中。 If you work with Rails databases from outside Rails itself, you should be careful about this value. 如果您从外部Rails本身使用Rails数据库,则应注意此值。

The "id" field is a key part of the Rails magic because it is used to define Rails' associations. “ id”字段是Rails魔术的关键部分,因为它用于定义Rails的关联。 Say you relate two tables together --- Group and Person. 假设您将两个表关联在一起---组和人。 The Group model will have an "id" field, and the Person model should have both its own "id" field and a "group_id" field for the relationship. 组模型将具有“ id”字段,而人模型应具有其自身的“ id”字段和用于关系的“ group_id”字段。 The value in "group_id" will refer back to the unique id of the associated Group. “ group_id”中的值将引用回关联组的唯一ID。 If you have built your models in a way that follows those conventions of Rails, you can take advantage of Rails' associations by saying that the Group model "has_many :people" and that the Person model "belongs_to :group". 如果您按照遵循Rails约定的方式构建模型,则可以通过说Group模型“ has_many:people”和Person模型“ belongs_to:group”来利用Rails的关联。

Rails migrations also, by default, want to add "created_at" and "updated_at" fields (the so-called "timestamps"), which are datetime fields. 默认情况下,Rails迁移也要添加日期时间字段“ created_at”和“ updated_at”字段(所谓的“时间戳”)。 By default, these take advantage of the "magic" in the database --- not in Rails itself --- to automatically update whenever a record is created or modified. 默认情况下,它们利用数据库中的“魔术”(而不是Rails本身)来在创建或修改记录时自动进行更新。 I don't think these columns will trip you up, because they should be taken care of at the database level, not by any special Rails magic. 我认为这些专栏不会让您失望,因为它们应该在数据库级别得到照顾,而不是由任何特殊的Rails魔术来处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这些导轨2的路线在导轨3中应该是什么样的? - What should these rails 2 routes look like in rails 3? Rails:外键 - 数据库表是什么样的? - Rails: foreign keys - how does the DB table looks like? 与rails(堆栈)目录相比,MEAN(堆栈)Node.js应用程序目录的外观如何? - What does a MEAN (stack) Node.js app directory look like in comparison to a rails (stack) directory? act_as_votable的视图看起来如何? 铁轨新手。 - How what does the view look like for act_as_votable? New to rails. 在Rails中自动保存时,我的HTML需要什么样? - What does my HTML need to look like when I autosave in Rails? Ruby on rails - 在处理集合时控制器应该是什么样子? - Ruby on rails - what should a controller look like when dealing with collections? Rails应用程序和移动应用程序的架构到底是什么样的? - What exactly will the architecture for a Rails app and mobile app look like? Rails应该是什么样的正确CSS文件模板? - What is the correct CSS file template for rails supposed to look like? 在rails中,默认的getter和setter会是什么样子? - What would a default getter and setter look like in rails? Rails关联是否会阻止像SQL创建表上的foreign_key那样创建新记录? - Does rails association prevents a new record to be created like a foreign_key on a SQL create table would?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM