简体   繁体   English

Rails是否为每个应用程序创建单独的数据库

[英]Does Rails create a separate database for each application

I am a beginner in Rails, trying some examples on my Windows 7 machine. 我是Rails的初学者,正在Windows 7计算机上尝试一些示例。

I am getting the following error : SQLite3::SQLException: table "entries" already exists 我收到以下错误: SQLite3::SQLException: table "entries" already exists

"entries" is a table that was already created in another Rails application, and I am trying a similar example that is trying to create "entries" again. “ entries”是已经在另一个Rails应用程序中创建的表,我正在尝试一个类似的示例,试图再次创建“ entries”。

Does Rails create a separate database in sqlite for each application ? Rails是否在sqlite中为每个应用程序创建一个单独的数据库? (I am new to Sqlite also). (我也是Sqlite的新手)。

By default, when using sqlite, Rails uses databases named development.sqlite3 , test.sqlite3 , production.sqlite3 which are files in the db directory of the Rails project. 默认情况下,在使用sqlite时,Rails使用名为development.sqlite3test.sqlite3production.sqlite3数据库,它们是Rails项目的db目录中的文件。

So you'll end up using the same database if your new application is in the same folder as your old one; 因此,如果新应用程序与旧应用程序位于同一文件夹中,则最终将使用同一数据库。 or if your config/database.yml file is pointing to some common location for the DBs. 或者您的config/database.yml文件指向config/database.yml某个公共位置。

For each application there is a database.yml file in config folder of your application. 对于每个应用程序,您的应用程序的config文件夹中都有一个database.yml文件。 You can define your database name unique to your application together with your database settings. 您可以定义应用程序唯一的数据库名称以及数据库设置。 Probably you just have the same database.yml file for both of your projects. 可能两个项目都只有相同的database.yml文件。 So your current application is also trying to create the table which is already existed(created by database.yml file of your previous application). 因此,您当前的应用程序还试图创建已经存在的表(由上一个应用程序的database.yml文件创建)。

The solution is to change the database.yml specific to your application. 解决方案是更改特定于您的应用程序的database.yml

After that you can run: 之后,您可以运行:

rake db:create

rake db:migrate

in the above order and it will be all fine. 按照上面的顺序,一切都会好的。

You can also do only rake db:drop and then the above two commands without modifying your any database.yml file but this will delete your previous database and create the new one for this application fresh. 您也可以只执行rake db:drop命令,然后执行上述两个命令,而无需修改任何database.yml文件,但这将删除您以前的数据库并为该应用程序重新创建新的数据库。 But this is not recommended. 但是不建议这样做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM