The used table type doesn't support FULLTEXT indexes (SQL: ALTER TABLE product_translations ADD FULLTEXT(name)) - sql

You can help me with this error i dont know to change to MyISAM engine, I see other post with this error.
SQLSTATE[HY000]: General error: 1214 The used table type doesn't support FULLTEXT indexes (SQL: ALTER TABLE product_translations ADD FULLTEXT(name))
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductTranslationsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('product_translations', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->string('locale');
$table->string('name');
$table->longText('description');
$table->text('short_description')->nullable();
$table->unique(['product_id', 'locale']);
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
DB::statement('ALTER TABLE product_translations ADD FULLTEXT(name)');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('product_translations');
}
}

Related

SQLSTATE[23000]: Integrity constraint violation 1452 Cannot add or update a child row: a foreign key constraint fails

I have this migration file
class AddRolesFieldsToUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreignId('role_id')->constrained();
$table->string('student_address')->nullable();
$table->string('student_licence_number')->nullable();
$table->string('teacher_qualifications')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
And a User model has all fillables like seen below
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use HasProfilePhoto;
use Notifiable;
use TwoFactorAuthenticatable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
'role_id',
'student_address',
'student_licence_number',
'teacher_qualifications',
];
But I get the SQLSTATE[23000]: Integrity constraint violation 1452 Cannot add or update a child row: a foreign key constraint fails when I try to register a new user, how should I fix this?
To solve this, manually insert the roles value into the database.
So for the first row:
id=2 | name=Teacher | created_at =0000-00-00 00:00:00|updated_at 0000-00-00 00:00:00
Second row:
id=3 | name=Student| created_at =0000-00-00 00:00:00|updated_at 0000-00-00 00:00:00

i wanna do php artisan db:seed, then i got this error " ErrorException array_merge(): Expected parameter 2 to be an array, int given"

I wanna make API Authentication Laravel Passport - Forgot and Reset Password with this source video on Youtube : https://www.youtube.com/watch?v=F9Xmc3iHc88&t=6s
My source Youtube use Laravel 6x, and i use Laravel 8x.
When i do step "seeding and factory database" in minute video 8:17 , i got
ErrorException array_merge(): Expected parameter 2 to be an array, int given
This is my error cmd
this is my UserFactory.php :
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'email' => $this->faker->unique()->safeEmail,
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
];
}
}
this is my UsersTableSeeder.php :
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\User;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
User::factory(App\Models\User::class, 10)->create();
}
}
and my DatabaseSeeder.php :
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
Can someone help me to explain why i got this error merge array?
Case Closed guys, in Laravel 8x u dont need type "
User::factory(App\Models\User::class, 10)->create();
just type :
User::factory(10)->create();
because u already call it User at first word..

SQLSTATE[HY000]: General error: 1 unknown column "user_id" in foreign key definition

I have this error when i run :
php artisan migrate:fresh
Illuminate\Database\QueryException : SQLSTATE[HY000]: General
error: 1 unknown column "user_id" in foreign key definition (SQL:
create table "users" ("id" integer not null primary key autoincrement,
"name" varchar not null, "email" varchar not null, "username" varchar
not null, "email_verified_at" datetime null, "password" varchar not
null, "remember_token" varchar null, "created_at" datetime null,
"updated_at" datetime null, foreign key("user_id") references
"users"("id") on delete cascade))
I'm following a video tutorial on youtube, and the code of the tutorial is this:
<?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->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('profiles');
}
}
If I copy and paste this code I have the error. So i searched on stackoverflow and I've found this solution:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->index('user_id');
});
Schema::table('profiles', function (Blueprint $table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('profiles');
}
This is my users table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('users');
}
But today, when i run php artisan migrate:fresh I had this error again.
How can I solve?
Thanks
As others have mentioned, user_id isn't a column in your users table, but you are trying to create an index on it. This line:
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
belongs in the profiles table creation schema and not in the users table creation schema.
The full code:
// create_users_table.php
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
// create_profiles_table.php <-- migrate AFTER users table
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
The error is clearly mention here:
foreign key("user_id") references "users"("id") on delete cascade)
and you don't have a column with name user_id
The syntax is:
CONSTRAINT constraint_name
FOREIGN KEY foreign_key_name (columns) <-- this must be a column in the table
REFERENCES parent_table(columns)
ON DELETE action
ON UPDATE action
but in your case user_id column is not available in the query. So either add the that column or remove this constraint code and try again.
The reason for this problem is that the profile table was created before the user table.
I have a trick for problem solve.
You should rename migration files in the migration directory.
for example let my file names be like this
2019_06_10_000001_create_users_table.php
2019_06_10_000002_create_profiles_table.php
first user table is created when you run "artisan migrate or migrate:fresh"
why? because first created migration file is user file.You should look carefully
2019_06_10_000001 -> users table name
2019_06_10_000002 -> profiles table name
Solution:
So u just need to do this: rename "user" table and name it numerically smaller than the "profiles" table.So problem will solve.
Other solution:
delete all migration files after run them respectively this commands.
php artisan make:migration create_users_table
php artisan make:migration create_profiles_table
Try this;
public function up()
{
Schema::dropIfExists('profiles');
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->string('image')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
That is the migration. Before you are able to run it; delete the row for 'CreateProfilesTable' in the migrations table.

Execute sql script when updating database schema Doctrine

Is it possible to append (or execute) a custom sql queries when executing:
app/console doctrine:schema:update --force
I have a script that create all my views and I want them to be updated whenever I update the database schema.
Sure, you can extend the UpdateSchemaCommand command and inject the EntityManager into by defining the command as a service.
The command:
// src/AppBundle/Command/CustomUpdateSchemaCommand.php
<?php
namespace AppBundle\Command;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
class CustomUpdateSchemaCommand extends UpdateSchemaDoctrineCommand
{
/** #var EntityManagerInterface */
private $em;
/**
* #param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
parent::__construct();
}
/**
* {#inheritDoc}
*/
protected function configure()
{
parent::configure();
}
/**
* {#inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Hello world');
$conn = $this->em->getConnection();
$conn->exec(/* QUERY */);
return parent::execute($input, $output);
}
}
The service:
// app/config/services.yml
app.command.custom_schema_update_command:
class: App\SportBundle\Command\CustomUpdateSchemaCommand
arguments: ["#doctrine.orm.entity_manager"]
tags:
- { name: console.command }
Hope this helps.

Create sql query in sonata admin

I use sonata admin in my project. i created two admin classes exam and student. i want to create an action in exam class that shows all students that should pass the exam after creating a new exam, this is the sql query that i want:
"Select * from student,exam where student.codeAdministration=exam.codeAdministration"
examAdmin.php:
<?php
namespace Exam\ExamBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
class ExamAdmin extends Admin
{
////
protected function configureFormFields(FormMapper $formMapper)
{
//////
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
////
}
protected function configureListFields(ListMapper $listMapper)
{
/////
}
protected function configureShowField(ShowMapper $showMapper)
{
//////
}
}
i tried this query without putting the condition of WHERE but it does not work:
protected function configureListFields(ListMapper $listMapper)
{
$query = $this->modelManager->getEntityManager()->createQuery('SELECT s FROM Exam\ExamBundle\Entity\student s');
$listMapper
->add('student', 'sonata_type_model', array('required' => true, 'query' => $query))
}
How can i create this action with this query??
exam.php:
<?php
namespace Exam\ExamBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Exam
{
/**
* #var integer
*/
private $idExam
////////
/**
* #var \Examens\ExamensBundle\Entity\Administration
*/
private $codeAdministration;
//////
public function getIdExam()
{
return $this->idExam;
}
///////
public function setCodeAdministration(\Exam\ExamBundle\Entity\Administration $codeAdministration = null)
{
$this->codeAdministration = $codeAdministration;
return $this;
}
/**
* Get codeAdministration
*
* #return \Exam\ExamBundle\Entity\Administration
*/
public function getCodeAdministration()
{
return $this->codeAdministration;
}
///////
}