在Laravel 10中,你可以在迁移中创建一个新的数据库吗?

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月15日 14:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定