I'm trying to migrate my database to an empty database,
but I get the following error when I do php artisan migrate:
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal-for-fun.permissions' doesn't exist (SQL: select *
from `permissions`)
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal-for-fun.permissions' doesn't exist
All migrations can be found here: https://github.com/cskiwi/portal-for-fun/tree/master/database
but I think the problem lies here:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTables extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up() {
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->integer('role_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
Schema::create('permission_user', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->primary(['permission_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down() {
Schema::dropIfExists('permission_user');
Schema::dropIfExists('role_user');
Schema::dropIfExists('permission_role');
Schema::dropIfExists('permissions');
Schema::dropIfExists('roles');
}
}
the database itself only contains an empty migration table
Create permission model in your project.
If you have permission model make sure that name in permission not Permission
I've had similar problem. Try to seaprate migrations and move part of them (those which use permissions table) from migrations folder. First, you should run migration which will create permissions table with php artisan migrate. Then move back migrations which create permission_role and permission_user tables to the migrations folder and run php artisan migrate again.
That helped me, hope it'll help you too.
Related
I got the following error. How can I fix it?
Base table or view not found: 1146 Table 'account_infos'
Migration
public function up()
{
Schema::create('account_info', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('users_id');
$table->foreign('users_id')->references('id')
->on('users')->onDelete('cascade');
$table->unsignedBigInteger('axis_id');
$table->foreign('axis_id')->references('id')
->on('axis')->onDelete('cascade');
$table->enum('account_type',['legal','rightful']);
$table->timestamps();
});
}
Just add:
protected $table='account_info';
in your model
when I try to do php artisan migrate it gives me this error
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table users add primary key users_user_id_primary(user_id))
I m pretty new to laravel tho
here is my migrations :
User migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id('user_id')->primary();
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
});
}
Post migration
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id('post_id');
$table->timestamps();
$table->string('content');
$table->foreignId('user_id')->constrained();
$table->primary(['post_id', 'user_id']);
});
}
Categories migration
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id('id_cat')->primary();
$table->string('nom');
});
}
Marques migration
public function up()
{
Schema::create('marques', function (Blueprint $table) {
$table->id('marque_id');
$table->foreignId('cat_id')->constrained();
$table->string('designation');
$table->primary(['marque_id','cat_id']);
});
}
UserMarques migration
public function up()
{
Schema::create('user_marques', function (Blueprint $table) {
$table->foreignId('cat_id')->constrained();
$table->foreignId('marque_id')->constrained();
$table->timestamps();
$table->primary(['marque_id','user_id']);
});
}
Use $table->increments('user_id'); instead of $table->id('user_id')->primary();
and with others.
BTW, i prefer only id instead of with model name, so in this case i prefer this:
$table->increments('id');
Update for Laravel 7.x
Laravel 7.x migration comes with id method
public function id($column = 'id')
{
return $this->bigIncrements($column);
}
so in this case: you can use id like this
$table->id();
PS: increments key comes with primary key
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('last_name')->nullable();
$table->string('phone_number')->nullable();
$table->text('Biography')->nullable();
$table->string('address')->nullable();
$table->string('photo')->nullable();
$table->string('facebook_link')->nullable();
$table->string('twitter_link')->nullable();
$table->string('youtube_link')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
I would like to changed the user to another name but could not and migration is not working.
If you want to rename the table, you can create a new migration with this code:
Schema::rename('users', 'some_other_table_name');
Then run composer du and php artisan migrate commands.
Or, you could recreate the table by rolling back this migration php artisan migrate:rollback whcih will work if you have this line in the down() method:
Schema::drop('users');
Change the table name:
Schema::create('some_other_table_name', function (Blueprint $table) {
And then run php artisan migrate
https://laravel.com/docs/5.5/migrations
I create a migration
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label')->nullable();
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('permission_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->primary(['role_id' , 'permission_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['role_id' , 'user_id']);
});
Any what I write in CMD php artisan migrate and composer dumpautoload and php artisan serve and... This error is seen. and too I deleted database and I create a new database.
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'prj_roocket.permissions' doesn't exist (SQL: select * from permissions)
[PDOException] SQLSTATE[42S02]: Base table or view not found: 1146
Table 'prj_roocket.permissions' doesn't exist
This error is given by the function getPermissions in AuthServiceProvider (or somewhere else you defined your authentication service provider).
Probabily your function looks like this:
protected function getPermissions()
{
return Permission::with('roles')->get();
}
Try to replace the function getPermissions with:
protected function getPermissions()
{
try {
return Permission::with('roles')->get();
} catch (\Exception $e) {
return [];
}
}
and then run php artisan migrate again.
Note: with this fix you are not going to compromise the system security.
Here is Vehicles migrate :
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->integer('power');
$table->float('capacity');
$table->float('speed');
$table->integer('maker_id')->unsigned();
$table->timestamps();
});
Schema::table('Vehicles', function (Blueprint $table){
$table->foreign('maker_id')->references('id')->on('Makers');
});
}
Here is Makers Migrate :
public function up()
{
Schema::create('Makers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('phone');
$table->timestamps();
});
}
When I run artisan migrate , I got following error messages.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `api`.`#sql-14b0_71
` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter t
able `Vehicles` add constraint `vehicles_maker_id_foreign` foreign key (`ma
ker_id`) references `Makers` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `api`.`#sql-14b0_71
` (errno: 150 "Foreign key constraint is incorrectly formed")
I want to create two tables: Vehicles & Makers . In vehicles , maker_id is foreign key . I read the some references for laravel migration from various sources. But I can't find solution.
Pay attention to your migration files order, you have two solutions:
Change order: make Makers Migrate file first then Vehicles Migrate.
If Makers Migrate file comes after Vehicles Migrate file and you don't want to change order, move this:
Schema::table('Vehicles', function (Blueprint $table){
$table->foreign('maker_id')->references('id')->on('Makers');
});
To Makers Migrate file.
Now I delete makers migrate file and I run vehicles migrate file . Below is vehicles migrate file.
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->integer('power');
$table->float('capacity');
$table->float('speed');
$table->integer('maker_id')->unsigned();
$table->timestamps();
});
}
But I got the below errors
PHP Fatal error: Cannot redeclare class CreateVehicleTable in C:\xampp\htdocs\m
yownapi\database\migrations\2016_07_05_190215_vehicles_table.php on line 35
[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot redeclare class CreateVehicleTable
Now it is work properly with below codes
Makers Migrate
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class MakersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('Makers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('phone');
$table->timestamps();
});
Schema::table('Vehicles', function(Blueprint $table){
$table->foreign('maker_id')->references('id')->on('makers');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('Makers');
}
}
Vehicles Migrate
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class VehiclesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->float('power');
$table->float('capacity');
$table->integer('maker_id')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('Vehicles');
}
}
There is a simple way to do this. From your Vehicles migration, make the following changes
public function up()
{
Schema::create('Vehicles', function (Blueprint $table) {
$table->increments('serie');
$table->string('color');
$table->integer('power');
$table->float('capacity');
$table->float('speed');
$table->integer('maker_id')
$table->foreign('maker_id')
->references('id')->on('Makers');
$table->timestamps();
});
}
And since you will be adding the values form the id column in Makers, Laravel already has got your back, so there is really no need to add the unsigned() property there. Its still Okay if you add it there.
Hope I have answered your question. For More Details