使用Ruby on Rails 2在Sqlite中创建新表的最佳方法(The best way to create a new table in Sqlite using Ruby on Rails

编程入门 行业动态 更新时间:2024-10-13 18:27:04
使用Ruby on Rails 2在Sqlite中创建新表的最佳方法(The best way to create a new table in Sqlite using Ruby on Rails 2)

那么在Rails 2中在Sqlite数据库中创建新表的最佳方法是什么。我使用rake db:migrate命令创建了数据库。 所以我应该编写单独的sql脚本来创建数据库或以某种方式使用rake。 我不需要脚手架。

So what's the best way to create new tables in a Sqlite database in Rails 2. I have created the database using rake db:migrate command. So should I write individual sql scripts to create a database or use rake somehow. I don't need scaffolding.

最满意答案

基本上使用迁移。

有关如何使用迁移的一些有用帮助,请访问http://wiki.rubyonrails.org/rails/pages/understandingmigrations和http://wiki.rubyonrails.org/rails/pages/UsingMigrations 。 我使用的好的备忘单也可以在http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations上找到 。

基本上,迁移使用ruby代码为您创建数据库表。 更容易(至少在我看来)使用漂亮的ruby代码而不是SQL DDL - 它也会自动为你做各种事情(比如像rails需要的那样在你的所有表中添加id字段)。 然后,您可以使用rake任务将迁移实际应用于数据库。 迁移为您提供的另一个主要优点是它们是可反向的 - 因此您的数据库是版本化的,您可以轻松地从一个版本跳转到另一个版本。

Basically use migrations.

Some useful help on how to use migrations is available at http://wiki.rubyonrails.org/rails/pages/understandingmigrations and http://wiki.rubyonrails.org/rails/pages/UsingMigrations. A good cheatsheet that I use is also available at http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations.

Basically migrations use ruby code to create your database tables for you. It is far easier (in my opinion at least) to use nice ruby code to do this rather than SQL DDL - it also does various things automatically for you (like adding id fields to all your tables as rails requires). You can then use rake tasks to actually apply the migrations to your database. The other major advantage that migrations give you is that they are reverseable - so your database is versioned and you can easily jump from one version to another.

更多推荐

本文发布于:2023-08-01 21:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365849.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:建新   中创   方法   Rails   Ruby

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!