I have a few tables where I need tinyint fields which have to be unsigned and set to auto_increment.
The L5.3 Database : Migrations documentation does not have a method to define unsigned auto-incrementing tinyint.
I have tried to implement it using the DB::update(). My migration file for the table looks like:
public function up()
{
Schema::create('table_name', function (Blueprint $table)
{
$table->unsignedTinyInteger('field1');
$table->string('field2', 255);
$table->primary('field1');
$table->index('field2');
$field = "field1";
});
$this->addAutoIncrements($field);
}
public function addAutoIncrements($field)
{
DB::update('ALTER TABLE table_name MODIFY $field TINYINT UNSIGNED NOT NULL AUTO_INCREMENT');
}
When I try php artisan migrate with such a migration file, it migrates without any errors but is not assigning auto-increment.
How do I resolved this? Should I do the update as a complete new migration? Has anyone done it before?
The second parameter for the unsignedTinyInteger() method is a boolean to flag if it is an autoincrementing field. It defaults to false, so you just need to update your migration to pass in true.
$table->unsignedTinyInteger('field1', true);
As a future note, the tinyIncrements() convenience method was added in Laravel 5.4.16 to do exactly that.
Related
so i want to php artisan migrate:fresh but i get this error
Base table or view already exists: 1050 Table 'roles' already exists
even if i drop the database from phpmyadmin, clean the cache and create the database again it still show the same message the migration for the table rows is the following one:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration
{
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->tinyInteger('status');
});
}
public function down()
{
Schema::dropIfExists('roles');
}
}
the full error displayed:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'roles' already exists (SQL: create table roles (id bigint unsigned not null auto_increment primary key, name varchar(255) not null, guard_name varchar(255) not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
Why is that? and what can or should I do?
First drop roles table using this code Schema::dropIfExists('roles'); then create.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration
{
public function up()
{
Schema::dropIfExists('roles'); //added
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->tinyInteger('status');
});
}
public function down()
{
Schema::dropIfExists('roles');
}
}
If it already exists before you did artisan migrate it will of course say this. Did you do migrate and it broke half way? You can reset it or just delete the table and try again (if you are in local dev and can delete it).
php artisan migrate:reset
I need to start primary_key index from e.g. 1000. How can I do it in migration? Or maybe in a model (but as I understand yii2 doesn't know anything about what id row will have)?
I found a solution:
create a new migration and in up function add:
public function safeUp()
{
$this->execute("ALTER TABLE {table_name} AUTO_INCREMENT = 1000;");
}
I have items table with supplier_id column and the foreign key to that column. The column is not nullable and I want to make it nullable. So the up() method works:
$table->integer('supplier_id')->unsigned()->nullable()->change();
But I can't get down() method to work, always get the error:
Cannot change column 'supplier_id': used in a foreign key constraint 'items_supplier_id_foreign'
Latest attempt:
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('items', function (Blueprint $table) {
$table->dropForeign(['supplier_id']);
$table->integer('supplier_id')->unsigned()->nullable(false)->change();
$table->foreign('supplier_id')->references('id')->on('suppliers');
});
}
Any suggestions? I'm using Laravel 5.4
#apokryfos is right. See here
Before any changes are made to your table, it’s important to briefly go over what data can (and cannot) be specified within an existing column that you wish to alter to NOT NULL, ensuring that no row is allowed to have a NULL value in that column.
At first, you need fill all nullable values using seeds and then modify column. Or your can drop supplier_id and than using default() method fill all rows in the table.
Only dropping foreign key constraint is not enough if you're changing the column next, you have to also drop its index. Below is the migration down function
Schema::table('users', function (Blueprint $table) {
$table->dropForeign('users_role_id_foreign');
$table->dropIndex('users_role_id_foreign');
});
// Conflict with change on same Blueprint instance (strange)
Schema::table('users', function (Blueprint $table) {
$table->integer('role_id')->change();
});
Credits: Github
I'm having problems with running my migration. I have a mysql database with some tables. The specific table is product_blender. Some fields in the table are like this:
id (PK)
area_id (FK)
inhabitants (varchar)
heating_type_id (FK)
...
Now I would like to create another table called installateur_types. The table needs to contain a PK and a varchar field. I would also like to create a FK in product_blender table to the id of my newly created tabel.
This is what I've done:
Created migration to create a table:
public function up()
{
Schema::create('installateur_types', function(Blueprint $table)
{
$table->increments('id');
$table->string('type');
});
}
public function down()
{
Schema::drop('installateur_types');
}
Run the migration, this was successful. Table was created with correct fields.
Then I've created the migration to add a FK field to the product_blender table.
public function up()
{
Schema::table('product_blenders', function ($table) {
$table->integer('installateurtype_id')->unsigned();
$table->foreign('installateurtype_id')->references('id')->on('installateur_types')->onDelete('cascade');
});
}
public function down()
{
//
}
When I now run the migration I get the following error:
What am I doing wrong?
If your products_blender table is not empty, then when you add a new column which is not null (which is the default for eloquent), it will be assuming some default value on its own. This value may not be available in the table this new column is referring to, causing the foreign key constraint to fail.
One of the way to get around this is to give a default value to the new column or just make it nullable.
$table->integer('installateurtype_id')->unsigned()->nullable();
$table->foreign('installateurtype_id')->references('id')->on('installateur_types')->onDelete('cascade');
There is one other solution, which turns off this checks, which can be done using DB::statement('SET FOREIGN_KEY_CHECKS=0;'). Then again turn that one for future with DB::statement('SET FOREIGN_KEY_CHECKS=1;'). In you code you can do something like
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
$table->integer('installateurtype_id')->unsigned();
$table->foreign('installateurtype_id')->references('id')->on('installateur_types')->onDelete('cascade');
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
I have a column created as follows in my Laravel 4 migration:
public function up()
{
Schema::create(
'templates',
function (Blueprint $table) {
$table->increments('id');
// Omitted fields...
$table->smallInteger('pages')->default(0);
$table->timestamps();
$table->softDeletes();
}
);
}
When I try to save the entity using
$entity->save();
in my controller, I get an error for that particular column when the field value is empty:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'pages' at row 1
I can fix this by creating a mutator in the model like so:
public function setPagesAttribute($value)
{
if (empty($value)) {
$this->attributes['pages'] = 0;
}
}
But isn't Laravel / Eloquent supposed to be doing that in any case?
I'm just thinking, I'm doing something wrong here...
I've discovered a solution online: http://www.garethalexander.co.uk/tech/mysql-5-incorrect-integer-value-column-id-row-1
Simply do the following:
edit my.cnf
comment out the line
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
And run $ mysql.server restart
The problem is that an empty string is getting passed to the query for the Pages field (the default will only trigger if there's no value being passed for the field). I'm not sure how you're building your entity object, but you basically shouldn't be setting entity->pages at all when you want it to use the default value.
If you're getting pages via Input variables, using either Input::get('pages', 0) or Input::get('pages', null) should fix the problem.