1

I followed the document to create a migration, add a schema version, install migration and add schema codes into Install.php. Last I run the migrate up command, but no table is created. Anyone could tell me what is going wrong please?

5
  • You install.php is only executed during the installation of your plugin. Do you want to create a migration on installation or just as an update? Commented May 8, 2018 at 0:25
  • @RobinSchambach I want to create a new table. If I reinstall the plugin, I'm not sure if any data will be lost
    – yu xia
    Commented May 8, 2018 at 0:29
  • Usually not. Craft 3 won't delete any data on it's own. If it is your plugin and you didn't include custom logic to delete certain things you can delete the plugin without any issues and install it again. However.. You can just do (new Install())->safeUp(); as well Commented May 8, 2018 at 0:30
  • @RobinSchambach what if I just run ./craft migrate/create <migration_name> --plugin=<plugin-handle> to create a migration, and copy the schema code into its safeUp() function And run ./craft migrate/up --plugin=<plugin-handle> The table will be created in this way without re-install the plugin?
    – yu xia
    Commented May 8, 2018 at 1:37
  • is there a certain reason why you want to use the CLI? I never did that because the (new Install())->safeUp() method is way faster and easier. I never tried it your way Commented May 8, 2018 at 9:05

1 Answer 1

2

Not sure I'm following 100%, but in Craft 3, you can add the code necessary to create the new table in your plugin's Install migration.

That will run every time someone does a fresh installation of your plugin.

For the people that already have your plugin installed, you need to create a migration that also creates the table. The code for the table creation will be very similar in both places.

For the latter, in order for Craft to know a plugin has a new migration is wants to run, you'll need to bump your plugin's schema version number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.