12
votes

Trying to use sqlite in development environment. It seems to detect the environment correctly but when I try to migrate to development.sqlite I get exception thrown "database does not exist"

artisan command

php artisan migrate --env=development

bootstrap/start.php

$env = $app->detectEnvironment(array(

    'development' => array('localhost'),

));

app/config/development/database.php

<?php

return array(
    'default' => 'sqlite',

    'connections' => array(
            'sqlite' => array(
            'driver'   => 'sqlite',
            'database' => __DIR__.'/../database/development.sqlite',
            'prefix'   => '',
        )
    )
);

As far as I know laravel is supposed to create the file if it does not exist but since it didn't I tried manually creating the file and still get the exception thrown.

UPDATE: Maybe something not right with the env because the same thing happens if I try ':memory' for the database.

UPDATE 2: I tried running the sample unit test but add to TestCase.php

     /**
     * Default preparation for each test
     *
     */
    public function setUp()
    {
        parent::setUp(); // Don't forget this!

        $this->prepareForTests();
    }

    /**
     * Creates the application.
     *
     * @return Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        $unitTesting = true;

        $testEnvironment = 'testing';

        return require __DIR__.'/../../bootstrap/start.php';
    }

    /**
     * Migrates the database and set the mailer to 'pretend'.
     * This will cause the tests to run quickly.
     *
     */
    private function prepareForTests()
    {
        Artisan::call('migrate');
        Mail::pretend(true);
    }

And this too gives the same exception though the testing env is already shipped with laravel. So I'll see if I can find any new issues on that.

4

4 Answers

22
votes

Wow, typos and wrong paths.

Copying the sqlite array from config/database.php into config/development/database.php I forgot to change the path to the development.sqlite file from

__DIR__.'/../database/development.sqlite'

to

__DIR__.'/../../database/development.sqlite'

And for the in memory test it should have been

':memory:'

instead of

':memory'
5
votes

I noticed that my database.php file had the following

'sqlite' => [
    'driver' => 'sqlite',
    'database' => env('DB_DATABASE', database_path('database.sqlite')),
    'prefix' => '',
],

I changed it to read the following, and it worked just fine.

'sqlite' => [
    'driver' => 'sqlite',
    'database' => database_path('database.sqlite'),
    'prefix' => '',
],
2
votes

One of the problem which I faced was I use "touch storage/database.sqlite" in terminal, so database is created in Storage folder instead of database folder.

in my config/database.php path is database_path('database.sqlite')

'sqlite' => [
        'driver' => 'sqlite',
        'database' => database_path('database.sqlite'),
        'prefix' => '',
    ],

than I use command "php artisan migrate" which gave me error "Database (/Applications/MAMP/htdocs/FOLDER_NAME/database/database.sqlite) does
not exist."

so it's obvious database file is not in database folder as It was generated in Storage folder, so copy "database.sqlite" from storage folder or run command "touch database/database.sqlite"

Hope that helps.!!

1
votes

Well, my answer is kinda outdated, but anyway. I faced the same problem, but with Laravel 5, I am using Windows 7 x64. First I manually created SQLite database called 'db' and placed it into storage directory, then fixed my .env file like this:

APP_ENV=local
APP_DEBUG=true
APP_KEY=oBxQMkpqbENPb07bLccw6Xv7opAiG3Jp

DB_HOST=localhost
DB_DATABASE='db'
DB_USERNAME=''
DB_PASSWORD=''

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null`

I thought it would fix my problems, but the command line keeps telling me that database doesn't exist. And then I just checked the path to db in my database.php file and this is why I put database file into storage directory. But nothing changed. And finally I checked db's extension and it was .db, not .sqlite as default extension you see in your sqlite block in database.php. So this is how I reconfigured sqlite piece:

'sqlite' => [
    'driver'   => 'sqlite',
    'database' => storage_path().'/db.db',
    'prefix'   => '',
],

And of course don't forget to set sqlite as default database in your database.php file. Good luck!