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
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`))
Hmm... try adding a specific length to your strings, for example:
$table->string('name', 250);
Let me know the response after that
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();
});
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');
});
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.
Thanks for your answers, I'll try that first thing tomorrow and keep you informed!
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?
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!
But mine was first :'( .
No hard feelings, I'm just glad everything worked out for you in the end!
Happy coding!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community