Laravel 迁移 - 表前缀问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15885681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:41:18  来源:igfitidea点击:

Laravel Migrations - Table Prefix Issue

phpmigrationlaravellaravel-3

提问by darksoulsong

I'm building a dummy site to test Laravel 3.x.

我正在构建一个虚拟站点来测试 Laravel 3.x。

I'm creating my site migrations right now. Everything was doing just fine until the following error showed up:

我现在正在创建我的站点迁移。一切都很好,直到出现以下错误:

SQLSTATE[42s02]: Base table or view not found: 1146 Table 'databasenamehere.prefix_laravel_migrations' doesn't exist

The issue is that laravel all of a sudden started to prefix the 'laravel_migrations' table (when it is supposed to do it only with the other ones).

问题是 laravel 突然开始在 'laravel_migrations' 表前加上前缀(当它应该只与其他表一起做时)。

I wonder if I'm doing something wrong or if it is a known issue.

我想知道是我做错了什么还是这是一个已知问题。

I'm trying to run the following migration (using the php artisan migrate applicationcommand):

我正在尝试运行以下迁移(使用php artisan migrate 应用程序命令):

public function up()
{
    Schema::create('siteinfo', function ($table) 
    {
        $table->engine = 'InnoDB';
        $table->string('name');
        $table->string('title')->nullable();
        $table->string('corp_name')->nullable();
        $table->string('corp_addr')->nullable();
        $table->string('corp_phone')->nullable();
        $table->string('corp_city')->nullable();
        $table->string('corp_state')->nullable();
        $table->string('corp_email')->nullable();
        $table->string('main_url')->nullable();
        $table->timestamps();
    });
}

Any help would be great.

任何帮助都会很棒。

EDIT 1:

编辑 1:

  • I noticed some minutes ago that my tables got no prefix at all, even with the "prefix" configuration set correctly on the config/database.php file.
  • Everything works fine if I remove the prefix. I know that I can set the prefix manually in every migration I run, but well...
  • 几分钟前我注意到我的表根本没有前缀,即使在 config/database.php 文件中正确设置了“前缀”配置。
  • 如果我删除前缀,一切正常。我知道我可以在我运行的每次迁移中手动设置前缀,但是......

回答by Bala

In application->config->database.phpset the prefixas follows

application->config->database.php设置prefix如下

'mysql' => array(
'driver'   => 'mysql',
'host'     => 'localhost',
'database' => 'foodb',
'username' => 'root',
'password' => '',
'charset'  => 'utf8',
'prefix'   => 'ula_',       <-- this is where you need to set the table prefix
),

After setting this, migrate:resetand migrateit again I have done this way and its works perfect

设置在此之后,migrate:resetmigrate再次我已经做了这种方式和它的作品完美

回答by rc.adhikari

On Laravel 5.4.*, I ended up creating the artisan command to add table prefix on certain tables (not globally) using below handle method.

在 Laravel 5.4.* 上,我最终创建了 artisan 命令以使用以下句柄方法在某些表(非全局)上添加表前缀。

public function handle()
{
    $this->tablePrefix = 'tmp_';

    // Set table prefix
    DB::setTablePrefix($this->tablePrefix);

    $data = [
        '--path' => [
            'database/prefixed-migrations' // Directory Path to migrations which require table prefix 
        ],
        '--database' => 'cli',
        '--force' => true
    ];

    $this->call('migrate', $data); // Next call the migration

    Model::reguard();
}

Hope this helps if anyone looking to prefix on certain tables without setting it globally.

如果有人希望在某些表上添加前缀而不全局设置它,希望这会有所帮助。