将列类型更改为 tinyInteger
Change column type to tinyInteger
尝试在 Laravel 5.2 迁移中将数据列类型更改为 tinyInteger:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterTableNameTableChangeNotificationSentTinyint extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('table_name', function ($table) {
$table->tinyInteger('column_name')->default(0)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
我遇到一个错误:
Doctrine\DBAL\DBALException]
Unknown column type "tinyinteger" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types wit
h \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. Use Abstrac
tPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot so
me mapping information.
我是不是做错了什么?
试试这个
Schema::table('table_name', 函数 (蓝图 $table) {
$table->tinyInteger('column_name')->默认(0)->改变();
希望这能解决您的问题
DB::statement("ALTER TABLE table_name CHANGE COLUMN column_name column_name TINYINT UNSIGNED NOT NULL");
确实 Dbal 不支持 Doctrine tinyint
你可以阅读他们的文档 here
同样不幸的是,laravel 声明 tinyint
无法更改。检查 here
我需要有人证明这是错误的,因为我的一个项目因为这个问题而不得不使用 smallInteger。我在想也许 boolean()
可能是解决方案。不过我没试过。
这样做
Change tinyInteger to smallInteger
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\SmallIntType;
if (!Type::hasType('integer')) {
Type::addType('integer', SmallIntType::class);
}
我遇到了同样的问题并找到了这个 solution。它对我有用。但它向我提出了一个问题,为什么创作者不更新到 doctrine/dbal
包。也许这个解决方案在某些情况下会导致错误?希望有人在这个答案中解释。
可以使用boolean
吗?
或
$table->smallInteger('column_name')->tinyInteger('column_name')->unsigned()->change();
如果您尝试将非数字列转换为 int 列,您将收到此错误。无法转换值。
在将旧字符串值转换为对父 table 的 id 引用时,您可能 运行 进入此状态。
与其尝试更改现有列,不如创建一个新列并删除旧列:
// Add new int column
Schema::table('children', function (Blueprint $table) {
$table->unsignedTinyInteger('parent_id')->after('parent_slug');
});
// Convert old values to new
// Only runs on environments that already have data in db, by virtue of pulling all records from the parents table
foreach (\App\Parents::all() as $parent) {
\App\Child::where('parent_slug', $parent->slug)->each(function ($child) use ($parent) {
$child->update([ 'parent_id' => $parent->id ]);
});
}
// Drop old string column
Schema::table('children', function (Blueprint $table) {
$table->dropColumn('parent_slug');
});
This solution is only for empty tables. not if already populated.
只需删除并重新创建具有相同名称的列。
public function up()
{
// Drop and recreate because laravel don't allow to change to the tinyInteger type
Schema::table('your_table_name', function (Blueprint $table) {
$table->dropColumn(['rating']);
});
Schema::table('your_table_name', function (Blueprint $table) {
$table->tinyInteger('rating')->nullable()->after('some_column_name');
});
}
尝试在 Laravel 5.2 迁移中将数据列类型更改为 tinyInteger:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterTableNameTableChangeNotificationSentTinyint extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('table_name', function ($table) {
$table->tinyInteger('column_name')->default(0)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
我遇到一个错误:
Doctrine\DBAL\DBALException]
Unknown column type "tinyinteger" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types wit
h \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. Use Abstrac
tPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot so
me mapping information.
我是不是做错了什么?
试试这个 Schema::table('table_name', 函数 (蓝图 $table) { $table->tinyInteger('column_name')->默认(0)->改变();
希望这能解决您的问题
DB::statement("ALTER TABLE table_name CHANGE COLUMN column_name column_name TINYINT UNSIGNED NOT NULL");
确实 Dbal 不支持 Doctrine tinyint
你可以阅读他们的文档 here
同样不幸的是,laravel 声明 tinyint
无法更改。检查 here
我需要有人证明这是错误的,因为我的一个项目因为这个问题而不得不使用 smallInteger。我在想也许 boolean()
可能是解决方案。不过我没试过。
这样做
Change tinyInteger to smallInteger
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\SmallIntType;
if (!Type::hasType('integer')) {
Type::addType('integer', SmallIntType::class);
}
我遇到了同样的问题并找到了这个 solution。它对我有用。但它向我提出了一个问题,为什么创作者不更新到 doctrine/dbal
包。也许这个解决方案在某些情况下会导致错误?希望有人在这个答案中解释。
可以使用boolean
吗?
或
$table->smallInteger('column_name')->tinyInteger('column_name')->unsigned()->change();
如果您尝试将非数字列转换为 int 列,您将收到此错误。无法转换值。
在将旧字符串值转换为对父 table 的 id 引用时,您可能 运行 进入此状态。
与其尝试更改现有列,不如创建一个新列并删除旧列:
// Add new int column
Schema::table('children', function (Blueprint $table) {
$table->unsignedTinyInteger('parent_id')->after('parent_slug');
});
// Convert old values to new
// Only runs on environments that already have data in db, by virtue of pulling all records from the parents table
foreach (\App\Parents::all() as $parent) {
\App\Child::where('parent_slug', $parent->slug)->each(function ($child) use ($parent) {
$child->update([ 'parent_id' => $parent->id ]);
});
}
// Drop old string column
Schema::table('children', function (Blueprint $table) {
$table->dropColumn('parent_slug');
});
This solution is only for empty tables. not if already populated.
只需删除并重新创建具有相同名称的列。
public function up()
{
// Drop and recreate because laravel don't allow to change to the tinyInteger type
Schema::table('your_table_name', function (Blueprint $table) {
$table->dropColumn(['rating']);
});
Schema::table('your_table_name', function (Blueprint $table) {
$table->tinyInteger('rating')->nullable()->after('some_column_name');
});
}