I have the following migrations/tables: Languages
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLanguagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('languages', function (Blueprint $table) {
$table->string('id');
$table->string('language')->nullable();
$table->string('location');
$table->string('code')->nullable();
$table->integer('state');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('languages');
}
}
Users
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('id_style')->unsigned();
$table->foreign('id_style')->references('id')->on('styles');
$table->string('username')->unique()->index();
//$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('rol');
$table->integer('status');
$table->string('ip')->unique();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
So far so good but in this one that follows I get errors: Profiles
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->string('id');
//falta
//aqui deberia deicr que id es compuesta por id_user y id_langauge
$table->integer('id_user')->unsigned();
$table->foreign('id_user')->references('id')->on('users');
$table->string('id_language')->unsigned();
$table->foreign('id_language')->references('id')->on('languages');
//$table->primary('id')->unique('id_user', 'id_language');//PRUEBA POR EJECUTAR
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('profiles');
}
}
MISTAKE:
php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2019_02_19_201248_create_styles_table
Migrated: 2019_02_19_201248_create_styles_table
Migrating: 2019_02_19_201643_create_users_table
Migrated: 2019_02_19_201643_create_users_table
Migrating: 2019_02_19_202644_create_contacts_table
Migrated: 2019_02_19_202644_create_contacts_table
Migrating: 2019_02_19_203326_create_languages_table
Migrated: 2019_02_19_203326_create_languages_table
Migrating: 2019_02_19_204235_create_profiles_table
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `profiles` add constraint `profiles_id_language_foreign` foreign key (`id_language`) references `languages` (`id`))
at /Applications/MAMP/htdocs/Andr3yvlz/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| }
668|
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
2 PDOStatement::execute()
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
Please use the argument -v to see more details.
MacBook-Air-de-Andres:Andr3yvlz andr3yvlz$
And with-v
php artisan migrate -v
Migration table created successfully.
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2019_02_19_201248_create_styles_table
Migrated: 2019_02_19_201248_create_styles_table
Migrating: 2019_02_19_201643_create_users_table
Migrated: 2019_02_19_201643_create_users_table
Migrating: 2019_02_19_202644_create_contacts_table
Migrated: 2019_02_19_202644_create_contacts_table
Migrating: 2019_02_19_203326_create_languages_table
Migrated: 2019_02_19_203326_create_languages_table
Migrating: 2019_02_19_204235_create_profiles_table
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) def' at line 1 (SQL: create table `profiles` (`id` varchar(255) not null, `id_user` int unsigned not null, `id_language` varchar(255) unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at /Applications/MAMP/htdocs/Andr3yvlz/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| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) def' at line 1")
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452
2 PDO::prepare("create table `profiles` (`id` varchar(255) not null, `id_user` int unsigned not null, `id_language` varchar(255) unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'")
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:452
3 Illuminate\Database\Connection::Illuminate\Database\{closure}("create table `profiles` (`id` varchar(255) not null, `id_user` int unsigned not null, `id_language` varchar(255) unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'", [])
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:657
4 Illuminate\Database\Connection::runQueryCallback("create table `profiles` (`id` varchar(255) not null, `id_user` int unsigned not null, `id_language` varchar(255) unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'", [], Object(Closure))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
5 Illuminate\Database\Connection::run("create table `profiles` (`id` varchar(255) not null, `id_user` int unsigned not null, `id_language` varchar(255) unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'", [], Object(Closure))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459
6 Illuminate\Database\Connection::statement("create table `profiles` (`id` varchar(255) not null, `id_user` int unsigned not null, `id_language` varchar(255) unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'")
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:97
7 Illuminate\Database\Schema\Blueprint::build(Object(Illuminate\Database\MySqlConnection), Object(Illuminate\Database\Schema\Grammars\MySqlGrammar))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:264
8 Illuminate\Database\Schema\Builder::build(Object(Illuminate\Database\Schema\Blueprint))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:165
9 Illuminate\Database\Schema\Builder::create("profiles", Object(Closure))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:237
10 Illuminate\Support\Facades\Facade::__callStatic("create")
/Applications/MAMP/htdocs/Andr3yvlz/database/migrations/2019_02_19_204235_create_profiles_table.php:26
11 CreateProfilesTable::up()
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:360
12 Illuminate\Database\Migrations\Migrator::Illuminate\Database\Migrations\{closure}()
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:367
13 Illuminate\Database\Migrations\Migrator::runMigration(Object(CreateProfilesTable), "up")
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:178
14 Illuminate\Database\Migrations\Migrator::runUp("/Applications/MAMP/htdocs/Andr3yvlz/database/migrations/2019_02_19_204235_create_profiles_table.php")
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:147
15 Illuminate\Database\Migrations\Migrator::runPending([])
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:96
16 Illuminate\Database\Migrations\Migrator::run([])
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:71
17 Illuminate\Database\Console\Migrations\MigrateCommand::handle()
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
18 call_user_func_array([])
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
19 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
20 Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Object(Closure))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
21 Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), [])
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Container/Container.php:572
22 Illuminate\Container\Container::call()
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Console/Command.php:183
23 Illuminate\Console\Command::execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/symfony/console/Command/Command.php:255
24 Symfony\Component\Console\Command\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Console/Command.php:170
25 Illuminate\Console\Command::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/symfony/console/Application.php:901
26 Symfony\Component\Console\Application::doRunCommand(Object(Illuminate\Database\Console\Migrations\MigrateCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/symfony/console/Application.php:262
27 Symfony\Component\Console\Application::doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/symfony/console/Application.php:145
28 Symfony\Component\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Console/Application.php:89
29 Illuminate\Console\Application::run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
/Applications/MAMP/htdocs/Andr3yvlz/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:122
30
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
/Applications/MAMP/htdocs/Andr3yvlz/artisan:37
The error is due to type incompatibility since the method
unsigned()
is typical of type fieldsinteger
and it is adding it to a type fieldstring
The primary key must be indicated in the languages table since, being of type
string
, Laravel does not automatically recognize it as primary.Then it would be like this:
and the profiles table must be removed
unsigned
since this field is only for typesautoincrement
orenteros
. And it would be like this: