【问题标题】:Laravel Migrations - Table Prefix IssueLaravel 迁移 - 表前缀问题
【发布时间】:2013-03-30 22:09:41
【问题描述】:

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

我现在正在创建我的网站迁移。一切正常,直到出现以下错误:

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

问题是 laravel 突然开始为“laravel_migrations”表添加前缀(当它应该只与其他表一起使用时)。

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

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

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();
    });
}

任何帮助都会很棒。

编辑 1:

  • 几分钟前我注意到我的表根本没有前缀,即使在 config/database.php 文件中正确设置了“前缀”配置。
  • 如果我删除前缀一切正常。我知道我可以在每次运行的迁移中手动设置前缀,但是……

【问题讨论】:

  • prefixapplication -> config -> database.php 中有什么?
  • 我的表格前缀为“ma_”。因此,错误指向一个“ma_laravel_migrations”表,当然,它不存在......
  • 这种情况现在是否始终如一,还是仅在第 5 次迁移时发生?如果您回滚并再次运行迁移会发生什么?
  • @darksoulsong 虽然我希望在迁移中应用前缀,但我实际上在 Schema 源代码中找不到任何可以证实这一假设的东西 - 也就是说它看起来像 Migrations/Schemas不要尊重前缀。自从您第一次 migrate:installed 以来,您是否更改/设置了前缀?
  • @PhillSparks 不知道发生了什么,但我删除了所有表并重新安装了 laravel 迁移......现在一切看起来都很好。 TYVM 为您提供帮助,先生。 =)

标签: php migration laravel laravel-3


【解决方案1】:

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
),

设置后,migrate:resetmigrate 再次设置 我已经这样做了,它的工作很完美

【讨论】:

    【解决方案2】:

    在 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();
    }
    

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

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 2017-07-14
      • 2017-01-22
      • 2019-08-01
      • 2016-09-17
      • 2019-05-24
      • 2014-08-09
      • 2018-10-18
      • 2017-09-19
      相关资源
      最近更新 更多