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

General error: 1215 Cannot add foreign key constraint #188

Closed
divspace opened this Issue Apr 10, 2016 · 7 comments

Comments

Projects
None yet
5 participants
@divspace
Copy link

divspace commented Apr 10, 2016

I'm getting the following error when I start a new project:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `dashboard__widgets` add constraint dashboard__widgets_user_id_foreign foreign key (`user_id`) references `users` (`id`) on delete cascade)

[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

The issue is with this file:

Modules/Dashboard/Database/Migrations/2015_04_02_184200_create_widgets_table.php

On line 21:

$table->foreign('user_id')->references('id')->on(config('auth.table', 'users'))->onDelete('cascade');

See the attached screenshot as well:

untitled-1 fw

@mikemand

This comment has been minimized.

Copy link
Contributor

mikemand commented Apr 10, 2016

This error means that the auth table (by default users) does not exist. You need to run php artisan asgard:install before doing anything else.

@tagmeo

This comment has been minimized.

Copy link

tagmeo commented Apr 10, 2016

This happens automatically though after Composer is done running. Even if you run the installer manually or the forced migrate module it still fails.

@hannesbe

This comment has been minimized.

Copy link

hannesbe commented Apr 11, 2016

Having the same problem on Homestead/Vagrant. It looks to me the Composer installation is currently not working for any fresh install (at least with Composer version 1.0.0 2016-04-05 13:27:25).

composer.json includes some post-install-cmd & post-update-cmd scripts, including php artisan module:migrate --force (though this is strangely only present in the update scripts, it does run during install, resulting in the module migrations being run before the platform migration.

This also causes when not having the default forge database with user forgeuser & blank password on localhost to get a database connection error at this point.

If you did have or created this database & user and tried again (as I did), it tries to add a foreign key to the users table before it has been created ( migration & dashboard__widgets are the only tables present at this point), causing this error.

You can work around it like this:

  • Remove tables migration & dashboard__widgets from the database, so you have an empty database.
  • Run php artisan asgard:install (only now you can set the database settings using the wizard)
  • I'm not sure if this is still needed, but I also ran php artisan module:migrate --force again because it was interrupted by this error, returns nothing to migrate on all modules though.
@mikemand

This comment has been minimized.

Copy link
Contributor

mikemand commented Apr 11, 2016

Ahh, ok. I know what the problem is. Composer's behavior changed: composer/composer#5066 (comment)

It doesn't run post-install-cmd anymore if there is no composer.lock, but instead runs post-update-cmd. How unsettling...If you run composer install (or in this case, create-project) you should expect it to run any install scripts, and not update scripts...

@divspace

This comment has been minimized.

Copy link
Author

divspace commented Apr 11, 2016

If you run the following:

composer create-project asgardcms/platform PROJECT-NAME

It will fail as well:

Writing lock file
Generating autoload files
> php artisan clear-compiled
> php artisan stylist:publish
AdminLTE assets published.
Flatly assets published.
Assets published.
> php artisan module:publish
Published: Core
Published: Dashboard
Published: Media
Published: Menu
Published: Page
Published: Setting
Published: Translation
Published: User
Published: Workshop
> php artisan module:migrate --force
Running for module: core

 [PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)

Script php artisan module:migrate --force handling the post-update-cmd event returned with an error

So it's running the post-update-cmd' instead of thepost-install-cmd, and since there's no.env` file setup, it fails. If you create one and run it again, it fails.

@nWidart

This comment has been minimized.

Copy link
Member

nWidart commented Apr 11, 2016

Hello,

thank you for reporting the issue. This is indeed related to a changed behaviour in Composer.

User hannesbe suggested the following work around:

  • Remove tables migration & dashboard__widgets from the database, so you have an empty database.
  • Run php artisan asgard:install (only now you can set the database settings using the wizard)
  • I'm not sure if this is still needed, but I also ran php artisan module:migrate --force again because it was interrupted by this error, returns nothing to migrate on all modules though.

This is until I decide on what to do, most likely remove the post-update-commands in composer.json.

@nWidart

This comment has been minimized.

Copy link
Member

nWidart commented Apr 19, 2016

I removed the migrate command from the post update command, which fixes the create-project new behaviour.

https://github.com/AsgardCms/Platform/releases/tag/1.14.1

Thank you for reporting the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.