SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "migrations" already exists - php

Laravel Version: 5.1
PHP Version:5.6
Database Driver & Version: psql
Description:
I run php artisan migrate the first time, and it work perfectly fine.
Then:
I've added 1 more migration script to alter one of my table.
As soon as I ran php artisan migrate, I started to see this error.
I have this setting in my database.php
'default' => env('DB_CONNECTION', 'pgsql'),
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Please kindly let me know what else, I can provide.

It is an old question but i faced this problem recently and it seems after copying my migration table from another database owner is changed.
So check your database and table owners and make sure tables owned by same user.

The actual solution is to append 'schema' => 'public', to the end of the psql DB_CONNECTION.
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'schema' => 'public', <------- ADD HERE
],
Now, php artisan migrate works perfectly fine.

Related

Error while creating record in laravel - mongo Application

I am using a package for mongodb in laravel : "jenssegers/mongodb": "^3.8",
migrations are working fine, but when I try to create a record using controller, its throwing error
InvalidArgumentException
Database connection [mongoproject] not configured.
my database.php
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
my .env
DB_CONNECTION=mongodb
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=mongoproject
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=

Configuration error with MongoDB laravel

I am planning to use MongoDb along with my Laravel project. I am using the jensseger/mongodb extension for it.
I have configured everything. I am getting an error saying mongodb not configured.
This is how my database.php file looks like:
'connections' => [
# Our primary database connection
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Double check your configuration in config/database.php
Also make sure that you fire php artisan config:cache after doing the changes.
Be sure to have DB_CONNECTION=mongodb set in your .env file or just replace 'default' => env('DB_CONNECTION', 'mysql') with 'default' => 'mongodb'. I would go with the former, as it's safer to rely only on the .env, rather than hardcoding your default database connection.
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => ['database' => 'Database_Name']
],
After clear cache "php artisan config:cache"
go to .env
Add these lines:
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=your database name
MONGO_DB_USERNAME=your database username if you have
MONGO_DB_PASSWORD=your database password if you have

Laravel : random error : SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

I'm getting this error randomly when using my laravel application:
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
If I refresh the browser, I'm not getting this error. It's really weird because it's working correctly before and after.
My database credentials are stored in .env file
EDIT
Here the content of my file :
APP_ENV=local
APP_DEBUG=true
APP_KEY=6cYKHzpblHGfE3H0n6j2tSRjoqJsgcqd
DB_HOST=localhost
DB_DATABASE=gestion-inscriptions-v2
DB_USERNAME=root
DB_PASSWORD=
EDIT 2
And here the database.php : http://laravel.io/bin/bE95X
EDIT 3
Still got this error, even on other computer (working with wamp)
Thanks in advance
PS : I know there is already the same questions on SO but there is no correct answer that works for me so ...
According config/database.php and .env file that You show us everything is ok.
SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected
this means that database is not selected,
seems like have problem with existence of database,
or DB_NAME does not get in config database param.
There is some steps You've to check:
1) Check Your config/database.php file contents to have such content:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_ACADEMY'),
'database' => env('DB_DATABASE_ACADEMY'),
'username' => env('DB_USERNAME_ACADEMY'),
'password' => env('DB_PASSWORD_ACADEMY'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
],
'mysql3' => [
'driver' => 'mysql',
'host' => env('DB_HOST_ACADEMY'),
'database' => env('DB_DATABASE_ACADEMY_DATE'),
'username' => env('DB_USERNAME_ACADEMY'),
'password' => env('DB_PASSWORD_ACADEMY'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false
]
],
'migrations' => 'migrations',
'redis' => [
'cluster' => false,
'default' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0
]
]
];
2) Check Your .env file exist in Your app root and it's not .env.example
3) Check .env file to have this line, if Yes so delete and copy-paste it from here (maybe spelling issue):
DB_DATABASE=gestion-inscriptions-v2
p.s. because of in Your question files are correct need more debug info, screenshot and etc. to see that problem is not with .env or file system (maybe .env file cannot be read).
Just now i see that problem, and problem was in not saved .env file (was used VS code)

Laravel 5.2 Database [xxx] not configured, while being defined in database.php

So I have 3 mysql DB's defined in my database.php file. I had 2 for the longest time and everything worked fine. I have one titled mysql, one mysql1 and one mysql2. The first 2 work fine, I copied the second one, changed the name to mysql2 and added the proper parameters but it still always says "Database mysql1 not configured. Is there some sort of cache? or somewhere else I have to define it? I am calling the DB through a model like this.
protected $connection = 'mysql1';
This is my database.php for clarification
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
This was fixed by using the command
php artisan config:cache
I submitted a Laravel issue request to fix it since that absolutely should not be required to change the DB connections variable.
If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try to manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.

Seeding tenant database in Laravel 5

I am working on building a system using a main database and multiple tenant databases in laravel 5. I have a database migration and seed for the main database no problem, using php artisan migrate:refresh --seed.
Tenants then get their own database upon registration. I need to run a migration and seed on the tenants database.
The tenant migration files are stored in a separate folder. The migration runs (unfortunately on the main database) with the following command
\Artisan::call('migrate', [
'--path' => "database/migrations_system"
);
However I need the migration to occur on the tenant database, say DB_1.
I read the following should work
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'db_1'
]);
However I end up with an error
InvalidArgumentException in DatabaseManager.php line 238:
Database [db_1] not configured.
Stuck.... How can I specify the migration to run on a specific database?
UPDATE:
I have found that changing config/database.php and adding db_1 gets me past this error....
'db_1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db_1',
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
],
However this connection is unknown and needs to be done on the fly, which I am also stuck on how to accomplish....
UPDATE Again and working solution.... Modify the config on the fly...
$connections = \Config::get('database.connections');
$tenant_database = 'db_1'; //assign from your main database
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $tenant_database ,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections['tenant'] = $tenant_connection;
\Config::set('database.connections', $connections);
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'tenant'
]);
So it looks like I have answered my own question, however maybe some can comment on this procedure.
I'll put my own solution down....
1) Create the database name
For example I name the new database db_1 where 1 represents the ID of the system in the main database. I have considered storing a unique scrambled name in the main database. In the end that seemed like overkill.
2) Create the connection - for this I use the exact same name as the new database.
public function createConnection()
{
$connections = \Config::get('database.connections');
if(!isset($connections[$this->getSystemName()]))
{
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $this->getSystemName(),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections[$this->getSystemName()] = $tenant_connection;
\Config::set('database.connections', $connections);
//dd(\Config::get('database.connections'));
}
}
3) Create the database, which you do using your main connection
$sql = "CREATE DATABASE " . $this->tenant_name;
DB::connection('main_db')->statement($sql);
4)run the migration
$r = \Artisan::call('migrate', [
'--path' => "database/migrations_tenant",
'--database' => $this->tenant_name
]);
5) finally you can seed test data or load some default data. Access the connection like
DB::connection($this->system_name)->table($table)->insert($csv);

Categories