在 ruby on rails 中创建新表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16388756/
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
Create new Table in ruby on rails
提问by Eumundi
I try to create a new table in rails. Every example I find and try sadly does not work with me... so that's what I tried till now: (I use Ruby version 1.9 and Rails Version 3.2.13 making a new model in the terminal:
我尝试在 rails 中创建一个新表。我发现和尝试的每个示例都不适用于我......所以这就是我到现在为止尝试过的:(我使用 Ruby 1.9 版和 Rails 3.2.13 版在终端中创建一个新模型:
rails generate model content content_id:auto-generated, law_id:integer, parent_id:integer, titel:string, text:string, content:string, url:string
that generated following code:
生成以下代码:
class CreateContents < ActiveRecord::Migration
def change
create_table :contents do |t|
t.auto-generated, :content_id
t.integer, :law_id
t.integer, :parent_id
t.string, :titel
t.string, :text
t.string, :content
t.string :url
t.timestamps
end
end
end
if I try to rake db:migrate i get the following error message:
如果我尝试 rake db:migrate 我会收到以下错误消息:
syntax error, unexpected ',', expecting keyword_end
t.auto-generated, :content_id
^
if I remove the "," I get this error message:
如果我删除“,”我会收到此错误消息:
syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '('
t.auto-generated :content_id
^
my research got me to also to this way of creating a table:
我的研究使我也采用了这种创建表格的方式:
class CreateContents < ActiveRecord::Migration
def change
create_table :contents do |t|
t.auto-generated "content_id"
t.integer "law_id"
t.integer "parent_id"
t.string "titel"
t.string "text"
t.string "content"
t.string "url"
t.timestamps
end
end
end
if I try to rake the db with that example I get this error message:
如果我尝试使用该示例来耙 db,则会收到此错误消息:
syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
t.auto-generated "content_id"
^
What do I do wrong?
我做错了什么?
回答by Luís Ramalho
auto-generatedis not a supported column type.
auto-generated不是受支持的列类型。
Active Record supports the following database column types:
Active Record 支持以下数据库列类型:
:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:primary_key
:string
:text
:time
:timestamp
More info in http://guides.rubyonrails.org/migrations.html#supported-types
更多信息在http://guides.rubyonrails.org/migrations.html#supported-types
Rails will create the column id automatically for you, thus just edit your migration to the following
Rails 会自动为您创建列 id,因此只需将您的迁移编辑为以下内容
class CreateContents < ActiveRecord::Migration
def change
create_table :contents do |t|
t.integer "law_id"
t.integer "parent_id"
t.string "titel"
t.string "text"
t.string "content"
t.string "url"
t.timestamps
end
end
end
回答by giorgian
As others say, :auto-generatedis not a supported column type. Also, it is not a symbol, it's an expressionand it is parsed as :auto - generated.
正如其他人所说,:auto-generated不是受支持的列类型。此外,它不是一个符号,它是一个表达式,它被解析为:auto - generated.
回答by webdevguy
Don't put commas in your command line call to the rails generator, that's what puts those commas in your migration.
不要将逗号放在对 rails 生成器的命令行调用中,这就是将这些逗号放在迁移中的原因。

