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

change()ing a column via a migration ignores storedAs() #35789

Closed
iansltx opened this issue Jan 5, 2021 · 2 comments
Closed

change()ing a column via a migration ignores storedAs() #35789

iansltx opened this issue Jan 5, 2021 · 2 comments

Comments

@iansltx
Copy link
Contributor

iansltx commented Jan 5, 2021

  • Laravel Version: 8.18.2
  • PHP Version: 7.4
  • Database Driver & Version: MySQL 5.7 and 8.0

Description:

Specifying storedAs() (guessing virtualAs has the same problem) on a column change in a migration doesn't apply the generated column expression, silently leaving you with plain column rather than a generated one. This may be a Doctrine DBAL issue since it involves change(), but figured I'd file the issue where I experienced it.

Steps To Reproduce:

In one migration:

Schema::create('urls', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->json('response');
    $table->string('click_url')->storedAs("response->>'$.click_url'");
});

At this point you'll see the correct generated column in SHOW CREATE TABLE urls.

But once you run this:

Schema::table('urls', function (Blueprint $table) {
    $table->string('click_url', 4096)->storedAs("response->>'$.click_url'")->change();
});

You get the longer field length but lose the generated-ness.

@taylorotwell
Copy link
Member

Changing columns is handled basically entirely by Doctrine DBAL so probably needs to be discussed there.

@maaarghk
Copy link

fyi for anyone else affected, doctrine are waiting for a PR to add support to doctrine/orm for marking fields as not updateable / not insertable so that they can stop ignoring generated fields.

in the mean time, you could quite safely work around this as so:

Schema::create('urls', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->json('response');
    $table->string('click_url')->storedAs("response->>'$.click_url'");
});
Schema::table('urls', function (Blueprint $table) {
    $table->dropColumn('click_url');
});
Schema::table('urls', function (Blueprint $table) {
    $table->string('click_url', 4096)->storedAs("response->>'$.click_url'")->after('response');
});

since the column is generated there won't be any data loss.

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

No branches or pull requests

3 participants