I want to create a new table, users_profile
here is the migration code
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersProfileTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users_profile', function (Blueprint $table) {
$table->increments('id');
$table->string('city');
$table->string('country');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users_profile');
}
}
facing an error
base table or view already exists: 1050 Table "users" already exists
here is the migration code of users
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Although i am creating a new migration, a new table only i am declaring a primary key of users table as foreign key in users_profiles but facing the above error
help please!
Just a word of advice, Use Laravel Nomenclature to avoid these kind of issues. Always define your table names as plural snakecase and use singular camelcase model to access them.
In your case, change your table name to: user_profiles and use UserProfile model to access it automatically.
Ofocurse you need to change your class name in your migration accordingly: CreateUserProfilesTable
I believe it should resolve your issue.
I believe you are using : php artisan migrate and as users table already exists , running migration for users table again gives that error.
Use: composer dump-autoload and then php artisan migrate:refresh
to rollback all of your migrations and reinstall migrations.
Simple go to your database and drop Schema table ("users") and also remove entry in Schema table ("migrations") and run your migrations again...your problem is solve....
Related
I created two tables in database. But now I get error to create another one. Third table can not add in the database. But migrate successfully.
I tried all those thinks which are mentioned below:
php artisan make:migration create_book_table
php artisan migrate
after that I get an error:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `Admins` add unique `admins_emai
l_unique`(`email`))
Then I tried:
php artisan migrate:refresh
but it's still not fixed.
Here is the migration file
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBookTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('phone-number');
$table->string('checkIn');
$table->string('checkOut');
$table->string('Room');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
Put this line of code in your AppServiceProvider.php file in the boot function:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
can someone please help me with this one? I'm new to Laravel and when I try to do:
php artisan migrate
I get the error:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table posts add constraint posts_author_id_foreign foreign key (author_id) references users (id) on delete restrict)
and this is the post table content:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
I have noticed that when I comment this line:
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
the error is gone, so I guess the problem is in that line. So, can someone please tell me what will be the correct syntax or how to fix it?
The column data type must be the same on both tables for the foreign key to work. By default, the users id column is:
$table->bigIncrements('id');
Therefore, you need to use bigInteger for your posts migration:
$table->bigInteger('author_id')->unsigned();
Try to change the foreign ko to unsignedInteger
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('author_id');
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Is there any way/laravel-command to drop a specific table from the production server?
Set up a migration.
Run this command to set up a migration:
php artisan make:migration drop_my_table
Then you can structure your migration like this:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class DropMyTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
// drop the table
Schema::dropIfExists('my_table');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// create the table
Schema::create('my_table', function (Blueprint $table) {
$table->increments('id');
// .. other columns
$table->timestamps();
});
}
}
You can of course just drop and not check for existence:
Schema::drop('my_table');
Read further in the docs here:
https://laravel.com/docs/5.2/migrations#writing-migrations
You may also have to consider dropping any existing foreign keys/indexes, for example if you wanted to drop a primary key:
public function up()
{
Schema::table('my_table', function ($table) {
$table->dropPrimary('my_table_id_primary');
});
Schema::dropIfExists('my_table');
}
More in the docs in dropping indexes etc here:
https://laravel.com/docs/5.2/migrations#dropping-indexes
When I try to migrate tables I created I get this error
PHP Fatal error: Class 'Users' not found in /var/www/html/laravel/vendor/laravel/
framework/src/Illuminate/Database/Migrations/Migrator.php on line 301
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException",
"message":"Class 'Users' not found","file":"\/var\/www\/html\/laravel\/vendor
\/laravel\/framework\/src\/Illuminate\/Database\/Migrations\/Migrator.php",
"line":301}}
Here is my code:
Users table:
<?php
//Users Table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('email');
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down(){
}
}
Posts table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function(Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->string('m_keyword');
$table->string('m_disc');
$table->string('slug');
$table->integer('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
}
}
http://laravel.io/bin/zj31n
If you get the above error while running a migration, please run the below command
composer dump-autoload
For more info,
http://laravel.com/docs/master/migrations#running-migrations
Maybe what has gone wrong is that you did not call
php artisan migrate:rollback
before
php artisan migrate
Try to force delete the tables and try to migrate again.
And another thing. The 'user_id' column in the 'posts' table should be connected to the users table like this:
Schema::table('posts', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
and I would call the create the table column like this:
$table->unsignedInteger('user_id');
or
$table->integer('user_id')->unsigned();
I've created the database migration below. What I'd like to do is create an accounts table with an id as a primary key. However, I don't want the key to autoincrement starting at 1. Rather, I'd like it to autoincrement starting at 800500.
Is there a way to set the default value of a primary key like this?
I'm currently using Laravel v4.2.11 and sqlite v3.8.3.
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAccountsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('accounts', function(Blueprint $table)
{
$table->increments('id')->unsigned()->default(800500);
$table->string('name', 100);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('accounts');
}
}
The default method in the schema builder
Declare(s) a default value for a column
If you need the increment to start at a given value take a look at this answer.
You'd add the query to the migration:
public function up()
{
Schema::create('accounts', function(Blueprint $table)
{
$table->increments('id');
$table->string('name', 100);
$table->timestamps();
});
DB::statement("UPDATE SQLITE_SEQUENCE SET seq = 800500 WHERE name = 'accounts'");
}
There is no 'laravel' way to do this.