Error in migration with use prefix set in modal constructor

Posted 6 days ago by PetroGromovo

Hello, In my laravel 5.7 app with use of migration file I had to implement prefix of tables in database , After some tests I found a good way to set prefix of tables as config parameter, but leaving empty parameter database=>prefix in config/database.php. And I set prefi of tables in constructor of any model, like:


namespace App;

use Illuminate\Database\Eloquent\Model;

use DB;
use App\MyAppModel;
use App\Http\Traits\funcsTrait;
use Illuminate\Validation\Rule;

class Banner extends MyAppModel
    use funcsTrait;

    protected $primaryKey = 'id';
    public $timestamps = false;

    protected $fillable = [ 'text', 'url', 'active', 'ordering', 'view_type' ];

    public function __construct()
        $this->table = config('app.db_prefix').'banners';

But running migrations I found that eror was triggerer for some tables(not for all), like

Migrating: 2018_11_01_150912_create_banners_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1364 Field 'text' doesn't have a default value (SQL: insert into `banners` () values ())

  at /mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000]: General error: 1364 Field 'text' doesn't have a default value")

Commenting __construct above and setting line 
    protected $table = 'banners';

in the model above fixed this error, but similar problems on some other tables. Can you say why using of __construct raised this error ? If there is a better way for such prefix setting ?


Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.