Good Day
I have a Laravel 7 project that's having some issues, first i have this error
The solution that i found for that error is to edit 'strict' value to false in config/database.php. This fixed the error. But now i just realized that every time I create a user the value of the id is always "0" example
Here is my migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone_number');
$table->string('client_address');
$table->rememberToken();
$table->timestamps();
});
}
it is because of your id field in table is not AuthoIncrement
first of all remove users table in database
then remove create_users_table record in migrations table in database
and then change your migration to this
$table->bigIncrements('id');
and then php artisan migrate
Related
So im trying to connect 2 tables (users,drawing) in laravel,all of the other columns are being created,the only thing the compiler has problems with is the foreign key,
this is the code for both of the tables
Schema::create('users', function (Blueprint $table) {
$table->id('users_id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
Schema::create('drawings', function (Blueprint $table) {
$table->id('drawings_id');
$table->string('name');
$table->integer('canvas_size');
$table->foreign('users_id')
->references('users_id')->on('users')->onDelete('cascade');
$table->timestamps();
});
and this is the error I get when trying the command(php artisan migrate)
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'users_id' doesn't exist in table (SQL: alter table `drawings` add constraint `drawings_users_id_foreign` foreign key (`users_id`) references `users` (`users_id`) on delete cascade)
I tried different methods, the old one and new one,neither is working for me,can you please help, also I'm using OpenServer for the database
Solution:My problem was that I was using $table->unsignedInteger('users_id')->nullable();
instead of $table->unsignedBigInteger('users_id')->nullable();
for the users_id,sorry guys my bad
The problem is that you haven't added the user_id column in drawings table.
I will rewrite the two migration files according to the best practice:
Schema::create('users', function (Blueprint $table) {
$table->id(); // You have to leave the primary key "id" so leave this empty
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
Schema::create('drawings', function (Blueprint $table) {
$table->id();// You have to leave the primary key "id" so leave this empty
$table->string('name');
$table->integer('canvas_size');
// You have to add the foreign key column first
$table->unsignedBigInteger('user_id'); // Best to make it single
$table->foreign('user_id')
->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
Friends I have the following problem with laravel migrations using postgres, and when I make changes to a migration, in this case the users table, but I get an error trying to remove an index from a key, can you help me please with this problem.
This is my migration code:
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->integer('idProfile');
$table->string('name');
$table->string('surname');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists('users');
Schema::table('users', function (Blueprint $table){
$table->dropPrimary('id');
$table->dropIndex('users_pkey');
});
}
response from my console:
These are the indices that list me:
This is the structure of the final table:
Comments, things to improve I am all ears
When you are running migrate:refresh, you are running the down method. In your down method, you are dropping the table, and then trying to make edits to it, which is why you are getting "users" doesn't exist.
If this is just in a development env, make your changes in the up method, and remove everything apart from dropIFExists() from the down method.
It is highly recommended that don't change the migration file...
If you need to add a new row to your table (Consider you have mytable and you want to add myrow to the table), you can write in terminal :
php artisan make:migration add_myrow_to_mytable_table
after that , edit new added migration file!
remember to add the following code in down function :
Schema::table('mytable', function (Blueprint $table) {
$table->dropColumn('myrow');
});
after all, run :
php artisan migrate
If you want to remove a column from your table just follow this one :
Laravel Migrations - Dropping columns
I'm trying to make the id start from a certain value. It's working fine when I don't specify the startingValue(1000)
This is my products table schema
Schema::create('products', function (Blueprint $table) {
$table->id()->startingValue(1000);
$table->foreignId('store_id')->constrained()->onDelete('cascade');
$table->foreignId('category_id')->constrained()->onDelete('cascade');
$table->string('name');
$table->float('price');
$table->string('specification');
$table->string('description');
$table->timestamps();
});
This is my favorites table schema
Schema::create('favorites', function (Blueprint $table) {
$table->foreignIdFor(Product::class)->constrained()->onDelete('cascade');
$table->foreignIdFor(User::class)->constrained()->onDelete('cascade');
});
It gives me this error
Thanks 🙏
I am running this migration:
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('key');
$table->text('value')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
"Users" table migration (migrated at Laravel 5.8)
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('last_message_id')->nullable();
$table->timestamps();
});
Error that I receive:
Foreign key constraint is incorrectly formed
To me it seems like you ran the users migration with a previous version of laravel. Have a look with a db tool (like phpmyadmin, Sequel, etc.) what the actual data type of your users.id field is.
I assume it's not bigInteger so your solution could be to just use
$table->unsignedInteger('user_id');
I want to use two models on my laravel 7.x application : Users and Images :
# Users migration : 2014_10_12_000000_create_users_table.php
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
# Images migration : 2020_03_27_121254_create_models_images_table
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned;
$table->string('name');
$table->timestamps();
});
Schema::table('images', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
When trying to migrate i receive following error : General error: 1215 Cannot add foreign key constraint (SQL: alter table images add constraint images_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
I have already searched over google without success, someone could helps me please ?
Thank you
Problem
You are not setting the unsigned properly.
Solution
Replace:
$table->bigInteger('user_id')->unsigned;
By:
$table->bigInteger('user_id')->unsigned();
Explanation
Since the user_id is not unsigned, the definition does not match the id of the users table which means you cannot set the foreign key.
Before Laravel 7.x
You could also do:
$table->unsignedBigInteger('user_id');
Laravel 7.x
As of Laravel 7, you can do it like this in your users migration:
Schema::table('users', function (Blueprint $table) {
$table->id();
// ...
});
And in your images migration:
Schema::table('users', function (Blueprint $table) {
// ...
$table->foreignId('user_id')->constrained();
});
You can find more information here: https://laravel.com/docs/7.x/migrations#foreign-key-constraints
You are missing bracket in unsiged()
As per Laravel Documentation
->unsigned() Set INTEGER columns as UNSIGNED (MySQL)
Change
$table->bigInteger('user_id')->unsigned;
to
$table->bigInteger('user_id')->unsigned();