Laravel: Migration error when having an index - php

I am getting the following error when running migrations. The error in my terminal console is:
SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'referral_code' used in key specification without a key length (SQL: alter table `users` add index `users_referral_code_index`(`referral_code`))
Below is my actual migration file. I can't see to tell why it's happening, as I've used this pattern before with other migrations.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddReferralInfoToUsersTable extends Migration
{
public function up()
{
Schema::table('shop_users', function (Blueprint $table) {
$table->text('referral_code')->nullable();
$table->integer('referral_uses')->nullable();
$table->integer('referral_revenue')->nullable();
$table->index(['referral_code']);
});
}
}

Try changing the datatype of referral_code to string and set a length.
Schema::table('shop_users', function (Blueprint $table) {
$table->string('referral_code',100)->nullable();
}

You can try this:
Schema::table('shop_users', function (Blueprint $table) {
$table->mediumText('referral_code')->nullable();
}
or
Schema::table('shop_users', function (Blueprint $table) {
$table->string('referral_code',100)->nullable();
}
or
Schema::table('shop_users', function (Blueprint $table) {
$table->longText('referral_code')->nullable();
}

Related

Why I am getting this error? "Undefined variable: table", "C:\....database\migrations\2020_08_25_082835_create_featured_products_table.php", [])

I am developing in laravel with mysql database.
This is the error which I got.
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Undefined variable: table", "C:\xampp\htdocs\bizzcomputer\database\migrations\2020_08_25_082835_create_featured_products_table.php", [])
This product table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('prod_name');
$table->string('prod_brand')->nullable();
$table->unsignedBigInteger('category_id');
$table->timestamps();
$table->foreign('category_id')
->references('id')
->on('categories')
->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('products');
}
}
This featured_products table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFeaturedProductsTable extends Migration
{
public function up()
{
Schema::create("featured_products", function($table) {
$table->increments('id');
$table->integer('product_id');
$table->timestamps();
});
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
DB::unprepared('ALTER TABLE `featured_products` DROP PRIMARY KEY, ADD PRIMARY KEY ( `id` , `product_id` )');
}
public function down()
{
Schema::dropIfExists('featured_products');
}
}
What is the wrong with this?
Is it possible to use foreign key and composite key?
$table is undefined because your foreign key definition is coded outside of the Schema::create() context.
So move it back inside like this
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateFeaturedProductsTable extends Migration
{
public function up()
{
Schema::create("featured_products", function ($table) {
$table->increments('id');
$table->bigInteger('product_id');
$table->timestamps();
$table->foreign('product_id')
->references('id')
->on('products')
->onDelete('cascade');
});
DB::unprepared('ALTER TABLE `featured_products` DROP PRIMARY KEY, ADD PRIMARY KEY ( `id` , `product_id` )');
}
public function down()
{
Schema::dropIfExists('featured_products');
}
}

Laravel 7 migration :Syntax error or access violation: 1068 Multiple primary key defined

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

Can't Migrate Foreign Key

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

Method increments doesn't found in class. Laravel

there. I'm trying to deal with Laravel Framework, but there is a problem. Here is my code
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
});
}
I made migration and tried to add some fields to my table. But IDE doesn't recognize $table variable properly, so as a result I have warning: "Method increments doesn't found in class", and I can't use auto-completion.
Are there propositions how to fix this?
Whole code:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration {
public function up()
{
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title',150);
$table->text('body');
$table->string('preview',300);
$table->string('author',100);
$table->timestamps();
});
}
public function down()
{
Schema::drop('posts');
}
}
Just specify the type of $table. The class is Illuminate\Database\Schema\Blueprint
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
});

ErrorException: Creating default object from empty value

I created table migrations in Laravel using php artisan migrate:make. When I tried to create the tables in the database, I got an error:
[ErrorException]
Creating default object from empty value
What is this related to? No tables are created nor can I find any errors in my migrations.
I have 25 tables in the migrations folder, all look similar to this.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAddressesTable extends Migration {
public function up() {
Schema::create("addresses", function() {
$table->engine = "InnoDB";
$table->increments("id");
$table->integer("user_id")->unsigned();
$table->string("street");
$table->string("city");
$table->integer("postal_code")->unsigned();
$table->foreign("user_id")->references("id")->on("users");
$table->softDeletes();
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists("addresses");
}
}
Well you miss $table that you pass to the function.
Your schema create function needs to be in this style...
Schema::create('addresses', function(Blueprint $table)

Categories