英文:
Laravel 10, can you create a new database in a migration?
问题
我正在构建一个需要在一个PGSQL数据库服务器上拥有两个数据库的环境,一个是生产数据库,名为laravel,另一个是测试/开发数据库,名为laravel_test。是否有办法在Laravel中使用数据库迁移来创建新的数据库?我一直在尝试类似以下的方法:
$dbName = config("database.connections.pgsql-test.database");
// 创建第二个数据库
DB::statement("CREATE DATABASE $dbName");
但最终出现了以下错误:
SQLSTATE[25001]: Active sql transaction: 7 ERROR: CREATE DATABASE cannot run inside a transaction block (Connection: pgsql, SQL: CREATE DATABASE laravel_test)
在处理数据库特定操作方面,我经验有限,是否有一种方法可以使用主要的(pgsql)DB连接来迁移新的数据库,以便我可以使用这个连接(pgsql-test)?
(config/database.php)
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => 'prefer',
],
以便我可以使用这个连接(pgsql-test):
'pgsql-test' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge') . "_test",
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => 'prefer',
],
而不需要手动创建数据库?
英文:
I'm building an enviroment that needs two databases on one PGSQL database server for Laravel, the production one is named laravel, and the testing/dev one would be named laravel_test, is there any way to create the new database using database migrations in Laravel? I've been trying things like
$dbName = config("database.connections.pgsql-test.database");
// Create the second database
DB::statement("CREATE DATABASE $dbName");
but end up getting an error that states this:
SQLSTATE[25001]: Active sql transaction: 7 ERROR: CREATE DATABASE cannot run inside a transaction block (Connection: pgsql, SQL: CREATE DATABASE laravel_test)
I'm inexperienced when it comes to database specific operations like this, is there a way I could migrate a new Database using this primary (pgsql) DB connection:
(config/database.php)
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => 'prefer',
],
so that I could use this connection (pgsql-test):
'pgsql-test' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge') . "_test",
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => 'public',
'sslmode' => 'prefer',
],
without manually creating a database?
答案1
得分: 0
简单回答。不。你可以创建一个像评论中的一个命令,或者手动创建它。当第一次运行迁移时,你可能会被提示是否要创建模式。
英文:
Simple answer. No. You can create a command like one of the comments sais or create it manually. Also when running migrations first time, you may be prompted if you want the schema to be created.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论