Incorrect id after Eloquent save call - laravel-5

In the production environment which is using MariaDB 10.3.8, id is 0 after calling save method from model.
Table has an auto incrementing id column with unsigned integer type

Found the cause to be 'laravel/telescope' package after some googling.
After disabling that package it worked as usual.
More on this issue can be found in https://github.com/laravel/telescope/issues/289

Related

Specifying a name for a foreign key in Laravel migration when using the constrained() method

Laravel 8, database migrations. I know I can use the long-form :
$table->foreign('really_long_table_name_id', 'foreign_key_name')->references('id')->on('really_long_table_name')->onUpdate('cascade')->onDelete('cascade');
to create a foreign key with a specific name where (as in my case) the name that Laravel would automatically generate via the constrained() method :
$table->foreignId('really_long_table_name_id')->constrained()->onUpdate('cascade')->onDelete('cascade');
would be too long and cause MySQL to complain.
Is it possible to use the constrained() method and specify the foreign_key_name, rather than let it be dynamically generated? As I'm struggling to see it in the documentation, or any SO searches.
You can use the index method to update the name of the index.
$table->foreignId('really_long_table_name')
->constrained()
->cascadeOnUpdate()
->cascadeOnDelete()
->index('index_name');

How to create a get-graphql-schema enum from Hasura?

I am working on a project which is backed by Hasura. I am having difficulty creating an enum and getting it to auto-generate the values.
I've successfully created a migration according to the enum spec, and verified it is loading values into the database. Next I ran yarn hasura console, and from the console started tracking both tables I created & set BaseColor to be an enum type. I added a permission for public to SELECT from BaseColor.
Next I ran yarn hasura metadata export. That generated a tables.yml with BaseColor's table definition having is_enum true.
Then I ran, yarn update-schema (i.e. get-graphql-schema http://localhost:8080/v1/graphql > schema.graphql). The generated file is missing the BaseColor_enum I would expect to be present for an enum.
get-graphql-schema only generates an _enum for a table if that table is referenced by another.
So, if I add a foreign key that references BaseColor, it will generate BaseColor_enum, otherwise it won't.

Laravel 8 Migration - change enum values

I'm trying to change an enum value in database with Laravel migrations.
In first, i have tried this classic change :
Schema::table('questionnaires', function ($table) {
$table->enum('type', ['image', 'sound', 'video'])->nullable()->default('image')->change();
});
But I got the following error :
Unknown database type enum requested,
Doctrine\DBAL\Platforms\MySQL57Platform may not support it
I solved my problem by doing SQL directly :
DB::statement("ALTER TABLE questionnaires MODIFY COLUMN type ENUM('image', 'sound', 'video') DEFAULT 'image'");
But it does not seem optimal to me...
Is there a solution more in "agreement" with Laravel 8,
without going through pure SQL?
Thanks
Explanation
To elaborate a little bit more on this subject.
As noted here ENUMs cannot be reverse engineered into a certain type. Each ENUM is a value of it's own. Hence, you must specifically say to Laravel which type the ENUMs are.
This seems to be an issue with all versions of Laravel
In documentation for each version, i.e. https://laravel.com/docs/8.x/migrations#renaming-columns, you can find that changing enum fields is not supported. As noted here the DB statement inside your migration is the best workaround for now.
DB::statement("ALTER TABLE questionnaires MODIFY COLUMN type ENUM('image', 'sound', 'video') DEFAULT 'image'");
Migration files fix
This option is the above commented GitHub link which advises you to put this line of code before the actual migration inside up() method of migration file.
DB::connection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
Models/enum types fix
I think this one is a lot less straightforward then the above mentioned, but still exists as a valid option, so I will put it here,
You can read an official doctrine-project website page here which shows you how to this thing from within models / enum types.

Laravel Migration - Data type also changes when renaming column

I want to rename the column in database using migration, but when i tried it, the datatype also changes. How to rename column without changing the data type.
The current name of the column in the database is
payment_amount_cash and its type is double(10,2)
I tried
$table->renameColumn('payment_amount_cash', 'payment_amount_full')->comment('Advanced payment')->default("0.00");
to rename the column. Then after migration, the name was changed but the data type was also changed from double(10,2) to only double thus, making the default value change from 0.00 to 0.
I also tried to change the data type after the schema of renaming the column.
$table->renameColumn('payment_amount_cash', 'payment_amount_full')->comment('Advanced payment')->default("0.00");
$table->double('payment_amount_full', 10,2)->change();
but this one gives me error upon migration.
Doctrine\DBAL\DBALException : Unknown column type "double" 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 with \Doctrine\DBAL\Types\Type::getTypesMap().
If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type.
Use AbstractPlatform#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 some mapping information.
With this, I just want to know how to rename the column without affecting the data type. After migration, I would like to retain the data type of the column I just changed the name.
I learned that there is an issue in Schema regarding the enum and still in discussion until now.
I just used the other way to update the datatype of the column.
DB::statement('ALTER TABLE reservations MODIFY COLUMN payment_amount_full double(10,2)');
and this works for me. If there are other ways on how to do this, please let me know. Thanks.

The data reader is incompatible with the specified... A member of the type, 'Finalised', does not have a corresponding column in the data reader

I have added a column in my Sql Server Db then in VS i updated model from database, there a template auto generated some code.
The code compiled fine, but when i try access a the data base, the following error occurs:
The data reader is incompatible with the specified
'CompassModel.tbSalesContract'. A member of the type, 'Finalised',
does not have a corresponding column in the data reader with the same
name.
Finalised is the new column i added to my database.
Does anyone know how to track this down, and if so, is there a fix or methodology to follow to avoid in the future?
If you are using a SP then update the newly added column in that Or If you are using inline query put the exact name of the newly added column . The template you are talking about does not contain the newly added column name ..Try to edit the template ..the issue will be resolved.
If you are using a SP, then please make sure the alias names used.. ie. for eg: It should be written as
fieldname as 'aliasname'
thanks,
criss thomas
Hopefully this helps someone out but when I received this error I was trying to return an entity type from a stored procedure.
To fix this I had to add the correct function import mapping. In your edmx go to the Model Browser view and navigate to Funtion Imports. Right click your sproc name and select function import mapping. There you can map your sproc return results to the appropriate entity property.

Resources