Laravel 4: Migration - Schema::table - Text column automatically creates primary unique index - php

I am creating a migration in Laravel with the following function:
public function up()
{
Schema::create('translate_item', function(Blueprint $table)
{
$table->increments('id');
$table->integer('lesson_id');
$table->text('lang_1');
$table->text('lang_2');
$table->timestamps();
});
}
The above creates the text fields as primary unique index as shown below:
Can anyone tell why this is happenning and how to create text fields without making it primary index?

In fact they aren't created as primary key nor with unique index.
It maybe look like it. The fields are greyed out, because that's how phpMyAdmin displays columns you can't make primary key or unique.
No need to worry. text() does nothing you wouldn't expect.

Related

Laravel Migration create foreign key using foreignIdFor with specific data type?

So I've been trying to write a migration that creates a data table question_display_formats using tiny increments as you see below.
And then, adding new Foreign Key column to existing questions table, trying to use the foreignIdFor method as a shortcut that'd look nice
public function up()
{
Schema::create('question_display_formats', function (Blueprint $table) {
$table->tinyIncrements('id');
$table->string('format');
});
Schema::table('questions', function (Blueprint $table) {
$table->foreignIdFor(QuestionDisplayFormat::class)
->nullable(true)
->after('question_type_id')
->constrained();
});
}
Turns out, this errors out with
General error: 1215 Cannot add foreign key constraint
Which turns out because the foreignIdFor users a different data type (confirmed by manually matching them and running the erroring out SQL alter table statement).
I googled, read and tried to adjust by doing:
$table->mediumIncrements('question_display_format_id'); before the foreignIdFor line, which leads to error
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name
'question_display_format_id' (SQL: alter table questions add
question_display_format_id mediumint un signed not null
auto_increment primary key, add question_display_format_id bigint
unsigned null after question_type_id)
Is there a way to use foreignIdFor with the matching column size? or am I supposed to fall back on the classic way of first creating the column explicitly, then doing like $table->foreign('question_display_format_id')->references('id')->on('question_display_formats'); which I don't like because its very verbose and doesn't look good?
On the other hand, this is a one time used script.. lol would've been faster to just do it the old way! but I am curious to see how to do it right :)

how can I use the same foreign key twice in a single table?

I came across a problem. So I have this migration in Laravel:
{
Schema::create('transfers', function (Blueprint $table) {
$table->id();
$table->foreignId('from_inventory_id')->constrained();
$table->foreignId('to_inventory_id')->constrained();
});
}
What I am trying to do is to transfer products from an inventory to another. The problem is, if I name them the way I did above, Laravel won't recognize them as foreign keys because they have to be 'inventory_id', but I also can't have two columns named the same.
How can I name them whatever I want and apply to them the same foreign key?
You should use the old syntax:
$table->unsignedBigInteger('from_inventory_id');
$table->foreign('from_inventory_id')->references('id')->on('inventories');
$table->unsignedBigInteger('to_inventory_id');
$table->foreign('to_inventory_id')->references('id')->on('inventories');

strange thing about foreign key in laravel

I meet Strange Problem when I use foreignkey in laravel
When I used unsignedBigInteger Foreign key work
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
But if I just edit Datatype it to
$table->*unsignedInteger('user_id');
** or
$table->Integer('user_id');
Foreign key Not work
I don't know why this happend
if you want to make your id datatype unsignedInteger or Integer you need first to change the users id datatypes in its migration file to the same data type because of a foreign key must have the same data type between the two columns

laravel 5 migration add some primary keys to my database table. how to prevent form it?

When i create Laravel5 migration as follows it adds "linkdesc" column as a primary key. when i read the documentation of laravel5 migration it does not mentioned that $table->text('description'); this gives a primary key in database.
is there any way to prevent such automatically added primary keys in laravel5 ?
Also is there any other migration functionalities that give this kind of unwanted primary keys ?
my migration is as follows
Schema::create('articles', function (Blueprint $table) {
$table->primary(['pemail', 'linkid']);
$table->bigInteger('linkid');
$table->string('pemail');
$table->string('linkname');
$table->string('linkurl');
$table->integer('linorder');
$table->text('linkdesc')->nullable();
});
I think you misread the info you get in PhpMyAdmin. You have primary word inactive without possibility to click on it in the row for linkdesc column and you thought Laravel creates primary key for this column.
However the truth is quite different - it's inactive not because Laravel created automatically primary key for this field but because it's a text field and texts fields cannot be primary keys.
You can verify it when you click in PhpMyAdmin on Indexes - it should be below the table - there you will see there is no primary key on linkdesc column.

Add foreign key constraints in Laravel migration

I know a lot of people asked the same question many times but it seems as if I am not able to make the same solutions work for me.
I am not able to set foreign key for the related tables for which the up() has been given below:
//Migrate 1 Starts
public function up()
{
Schema::create("UserTable", function($table){
$table->tinyInteger("ApplicationID");
$table->bigInteger("UserID");
$table->string("UserName")->unique();
$table->timestamps();
});
}
//Migrate 1 Ends
//Migrate 2 Starts
public function up()
{
Schema::create("Membership", function($table){
$table->tinyInteger("ApplicationID");
$table->bigInteger("UserID");
$table->string("Password");
}
}
//Migrate 2 Ends
//Migrate 3 Starts: This has the latest timestamp, so it runs after all tables are created
public function up()
{
Schema::table('UserTable', function($table) {
$table->primary("UserID");
$table->foreign("UserID")->references("UserID")->on("Membership");
});
Schema::table('Membership', function($table) {
$table->primary("UserID");
$table->foreign("UserID")->references("UserID")->on("UserTable");
});
}
//Migrate 3 Ends
The error I am getting is SQLSTATE[HY000]: General rror: 1215 Cannot add foreign key constraint (SQL: alter table 'Membership' add constraint membership_userid_foreign foreign key ('UserID') references 'UserTable' ('UserID'))
The problem was that I was setting up primary key at the time of setting up foreign key. This was causing problem.
So setting up of primary columns must be done while creating the tables, while foreign keys must be added once all the related tables have been created.
I guess its better that we create a create_associations migration file after all the tables have been created so that this migration has the latest timestamp, and by the time this file is executed, all the tables are present in the database.

Categories