Support the ongoing development of Laravel.io →
Configuration Database Eloquent
Last updated 2 years ago.
0

code is a reserved word in sql I believe, can you change it to something else, like 'test_code'? This could be what's causing conflicts

Last updated 2 years ago.
0

Thanks for the answer.

I tried replacing 'code' by 'country_code' (and 'code' by 'continent_code' for the foreign key), but it doesn't seem to be the problem, as I get almost the same error message :

...(SQL: alter table `country` add primary key country_country_code_primary(`country_code`))
Last updated 2 years ago.
0

Hmm... try adding a specific length to your strings, for example:

$table->string('name', 250);

Let me know the response after that

Last updated 2 years ago.
0

It seems to have solved the main problem, but now the foreign key seems to make the migrate command mad :

[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'ressources.#sql-6e09_35' (errno: 150) (SQL: alter table `country` add constraint country_continent_code_foreign foreign key (`continent_code`) references `continent` (`contine
  nt_code`))

My 'country' migration file looks like that:

Schema::create('country', function(Blueprint $table)
                {
                        $table->string('country_code',2);
                        $table->string('name');
                        $table->integer('continent_code');
                        $table->timestamps();

                        $table->primary('country_code');
                        $table->foreign('continent_code')->references('continent_code')->on('continent');

                });

And my 'continent' migration file:

 Schema::create('continent', function(Blueprint $table)
                {
                        $table->increments('continent_code');
                        $table->string('name');
                        $table->timestamps();
                });
Last updated 2 years ago.
0

Hmm.. make sure to have your foreign key set to unsigned, here's an example:

    Schema::table('country', function(Blueprint $table)
    {
        $table->integer('continent_code')->unsigned();
        $table->foreign('continent_code')->references('continent_code')->on('continent');
    });
Last updated 2 years ago.
0

I think I ran into a problem like this once — try making the continent code column unsigned, I think that's what solved it for me.

$table->integer('continent_code')->unsigned();

You can read more about it in the Foreign Keys section in the manual.

Last updated 2 years ago.
0

Thanks for your answers, I'll try that first thing tomorrow and keep you informed!

Last updated 2 years ago.
0

Hello,

Apparently it's still not working. Country :

Schema::create('country', function(Blueprint $table)
{
    $table->string('country_code',2);
    $table->string('name');
    $table->integer('continent_code')->unsigned();
    $table->timestamps();

    $table->primary('country_code');
    $table->foreign('continent_code')->references('continent_code')->on('continent');
});

Continent :

Schema::create('continent', function(Blueprint $table)
{
    $table->increments('continent_code');
    $table->string('name');
    $table->timestamps();
});

And the error :

[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table 'ressources.#sql-6e09_37' (errno: 150) (SQL: alter table `country` add constraint country_continent_code_foreign foreign key (`continent_code`) references `continent` (`contine
  nt_code`))

I don't get it :(

Do I have to create the 'continent' table first? It seems logical but the migrate tool created 'country' first, maybe that's the issue?

Last updated 2 years ago.
0

Yeah, that was the trick.

'Simple' tables (no foreign keys) first. Had to backup the 'country' migration file, 'php artisan migrate' just for 'continent', and then again for 'country'.

And it seems that

$table->string('whatever_code', 2)->references('whatever_code')->on('whatever');

and

$table->string('whatever_code', 2);
$table->foreign('whatever_code')->references('whatever_code')->on('whatever');

don't do the exact same thing as in the 2nd case, my 'whatever_code' column is marked as 'MUL' in MySQL.

Anybody knows why?

PS: we can't mark multiple posts as solutions, sorry!

Last updated 2 years ago.
0

But mine was first :'( .

No hard feelings, I'm just glad everything worked out for you in the end!

Happy coding!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

SynRJ synrj Joined 3 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.