Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel won´t update column listing after migration which adds a new not null column and won´t allow insert into that column #40653

Closed
mesiarm opened this issue Jan 26, 2022 · 3 comments

Comments

@mesiarm
Copy link

mesiarm commented Jan 26, 2022

  • Laravel Version: 8.36.2
  • PHP Version: 8.0.15
  • Database Driver & Version: mysql:8.0

Description:

fill() method in Illuminate\Database\Eloquent\Model checks if attribute is fillable or not guarded. But isGuarded() in Illuminate\Database\Eloquent\Concerns\GuardsAttributes trait checks if column isGuardableColumn() an that method checks if column is in static::$guardableColumns, which is column listing from database (it is not well documented for isFillable() method). But static::$guardableColumns is only loaded once if not set and won´t update after running migration, if more migrations are migrated during one run of MigrateCommand command.
static::$guardableColumns should be updated after running Schema::table migration.

static::$guardableColumns could be moved to Illuminate\Database\Eloquent\Concerns\HasAttributes trait, renamed to $databaseColumns and checked in fill method in Illuminate\Database\Eloquent\Model class separately before $this->isFillable($key) checking for better readibility.

Steps To Reproduce:

  1. Create migration for table items and create model Item with guarded property id
    protected $guarded = ['id'];
  2. Create migration, which adds new column to existing table
Schema::table('items', function (Blueprint $table) {
            $table->string('slug');
        });
  1. Create another migration which creates new models of Item, fills all attributes including slug and saves.
$data = ['slug' => 'test'];
            $item = new Item();
            $item->fill($data);
            $item->save();
  1. Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1364 Field 'slug' doesn't have a default value is returned

Workaround:

$item = new Item();
$item->setRawAttributes($data);
$item->save();
@driesvints
Copy link
Member

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as separate commits on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@mesiarm
Copy link
Author

mesiarm commented Jan 30, 2022

When created new repository with current version of laravel framework, I could not reproduce the issue. I have found it was already fixed in newer version of laravel (#39880)

@mesiarm mesiarm closed this as completed Jan 30, 2022
@driesvints
Copy link
Member

@mesiarm glad you found a fix 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants