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
Related
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
I have searched for people who encountered this error but I still can't find the solution.
I have been getting the error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'material_tags.material_uuid' in 'field list' (SQL: select tags.*, material_tags.material_uuid as pivot_material_uuid, material_tags.tag_uuid as pivot_tag_uuid from tags inner join material_tags on tags.uuid = material_tags.tag_uuid where material_tags.material_uuid in (05a36470-d0a0-11e7-91b4-ff3d7d9f961a) and tags.deleted_at is null)"
in which if I have to view Material 05a36470-d0a0-11e7-91b4-ff3d7d9f961a it should look like this
When I try to run this code located on the controller:
public function show(Request $request, $id)
{
$material = Material::with('tags')->where(
'uuid',
$id
)->first();
where my Material Model has this:
public function tags()
{
return $this->belongsToMany('App\Models\Tag', 'material_tags');
}
So I have a Tags table where all the tags are stored, and a Materials table that has all the materials stored. and I have the Material_tags table to see what Materials have tags.
my create_materials_table at migration
public function up()
{
Schema::connection('materials')->create('materials', function (Blueprint $table) {
$table->uuid('uuid')
->primary();
$table->string('title');
$table->integer('viewing_time')
->default(15)
->comment('In seconds');
$table->text('description')
->nullable();
$table->uuid('organization_id')
->nullable();
$table->timestamps();
$table->softDeletes();
});
}
my create_tags_table migration
public function up()
{
Schema::connection('materials')->create('tags', function (Blueprint $table) {
$table->uuid('uuid')
->primary();
$table->string('name')
->unique();
$table->timestamps();
$table->softDeletes();
});
}
and my create_material_tags_table migration
public function up()
{
Schema::connection('materials')->create('material_tags', function (Blueprint $table) {
$table->uuid('uuid')
->primary();
$table->uuid('material_id');
$table->uuid('tag_id');
$table->timestamps();
$table->foreign('material_id')
->references('uuid')
->on('materials')
->onDelete('cascade');
$table->foreign('tag_id')
->references('uuid')
->on('tags')
->onDelete('cascade');
});
}
You have to specify the custom foreign keys:
public function tags()
{
return $this->belongsToMany('App\Models\Tag', 'material_tags', 'material_id', 'tag_id');
}
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.
I created two migrations, because I wanted to create a one-to-one relationship between Trend and TrendExplanation.
Schema::create('trend_explanations', function (Blueprint $table) {
$table->increments('id');
$table->text('demographical')->nullable();
$table->text('economical')->nullable();
$table->text('social')->nullable();
$table->text('technological')->nullable();
$table->text('ecological')->nullable();
$table->text('political')->nullable();
$table->timestamps();
});
This is the migration for the Trend model:
Schema::create('trends', function (Blueprint $table) {
$table->increments('id');
$table->integer('explanation_id')->unsigned();
$table->foreign('explanation_id')->references('id')->on('trend_explanations');
$table->string('name');
$table->date('date');
$table->text('description');
$table->timestamps();
});
So in the models I did the following additions:
class Trend extends Model
{
public function explanation()
{
return $this->hasOne(TrendExplanation::class);
}
}
And of course I created a seeder to test it out:
$trend = new Trend();
$trend->name = "Something";
$trend->description = "Description";
$explanation = new TrendExplanation();
// ...
$trend->explanation()->save($explanation);
$trend->save();
And when I run it, I receive the error: [Illuminate\Database\QueryException] SQLSTATE[42S22]: Column not found: 1054 Unknown column 'trend_id' in 'field list'.
How does this happen? I don't understand what I did wrong here.
You can specify the foreign and local keys when specifying the relationship. It usually goes
$this->hasOne(Class, foreign, local);
So your definition would be:
return $this->hasOne(TrendExplanation::class, 'id', 'explanation_id');
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.