database error after adding new column - mysql

I had created a login and register form. But I forgot to add a column. So, I wanted to add a new column named "contactNumber". For adding the column, I wrote a command named php artisan migrate:refresh in cmd.exe and also write a code at create_users_table in migration folder. But, the new column's value doesn't go to mysql database. What am I doing wrong?

You need to add your new column to your model, so in your User class you need to do this:
protected $fillable = [
'name', 'email', 'password', 'contactNumber'
];
Hope this help

You're trying to insert a new record in the database but the contactNumber column doesn't have a default value. In the migration file you can add nullable to that column, eg.
$table->string('contactNumber')->nullable();
or directly in the database from a GUI.

For this type of scenario i personally create new migration like this
php artisan migrate:make add_column_to_table --table="tableName"
Now to run the new migration run this command.
php artisan migrate
This will add new column in your table.Also have a look into this thread.
https://laracasts.com/discuss/channels/laravel/add-new-column-to-existing-table

Related

Laravel5.5: migration string to JSON

How to make a migration in Laravel 5.5 that change the datatype of a String to JSON? My column has image links and I want to change it to JSON to store more links.
I thought this was the best way?
change the string value of the column so it corresponds to JSON datatype
change the datatype of the column to JSON
I'm able to do the first step in mysql by:
UPDATE db.vendor_horses SET image= CONCAT('{"images":["', image, '"]}');
Thanks!
You can easily create a new migration with php artisan make:migration migration_name command.
Use the following to change the datatype
Schema::table('vendor_horses', function (Blueprint $table) {
$table->json('image')->change();
});
The change() method allows you to set nullable, modify data length and types. Read more about change()
this just supported mySql version 8 to up
Schema::table('table_name', function (Blueprint $table) {
$table->json('field_name')->change();
});
First, If you use laravel and change your database table field then you install composer require doctrine/dbal Read more about modifying a column in laravel
Simply create a new migration file from php artisan make:migration migration_name and then open newly created migration file
Schema::table('your_table_name',function(Blueprint $table){
$table->json('image')->change();
//make nullable to image field
$table->json('image')->nullable()->change();
//rename image field to other
$table->renameColumn('old_column_name', 'new_column_name');
})
The change() method allow you to set data length , change your column name, and make nullable

Doctrine 2.5 - Generate schema provider class from existing database

Question
Are there any existing tools able to generate an implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider reflecting the state of an existing database?
I googled for a while and looked at similar questions in SO but i couldn't find an answer. They are all related to Symfony + Doctrine ORM.
Why do i need this?
I want to use doctrine migrations to manage/track changes on an existing database. I cannot jump into using doctrine ORM because I would need to refactor the database and it would break other ( non php ) applications that depend on it.
I know i can use migrations without ORM, but i need to provide a concrete implementation of Doctrine\DBAL\Migrations\Provider\SchemaProvider (documentation), in my case this would mean to rewrite the entire database.
If i had the SchemaProvider generated for the first time to reflect the state of the database. Eg
<?php
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Migrations\Provider\StubSchemaProvider;
final class MySchemaProvider implements SchemaProvider {
public function createSchema()
{
$schema = new Schema();
$table = $schema->createTable('foo');
$table->addColumn('id', 'integer', array(
'autoincrement' => true,
));
$table->setPrimaryKey(array('id'));
//and so on for the rest of the databse...
return $schema;
}
}
I would be able to edit the generated class and create migrations with:
$ ./doctrine migrations:diff

Laravel seed database from existing database

i have now new structure of my database, but i need to import the old data in the new format. For that reason i want to use the Laravel seeder, but i need somehow to connect to the old database and make select queries and to tell the seeder how to put the data in the new database.
Is that possible ?
Try:
Examples:
php artisan iseed my_table
php artisan iseed my_table,another_table
Visit: https://github.com/orangehill/iseed
Configure your laravel app to use two mysql connections (How to use multiple database in Laravel), one for the new database, the other for the old one.
I'll fake it like old and new.
In your seeds read from the old database and write into the new.
$old_user = DB::connection('old')->table('users')->get();
foreach ($old_users as $user) {
DB::connection('new')->table('users')->insert([
'name' => $user->name,
'email' => $user->email,
'password' => $user->password,
'old_id' -> $user->id
// ...
]);
}
Make sure to add messages while seeding like $this->command->info('Users table seeded'); or even a progress bar (you can access command line methods) to know at which point of the import you are.
Download package from
Git repo : https://github.com/orangehill/iseed
then update below file src/Orangehill/Iseed/IseedCommand.php
Add below code at line number 75
// update package script
if($this->argument('tables') === null){
$tables = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
}
and update getArguments method in same file with below code
array('tables', InputArgument::OPTIONAL, 'comma separated string of table names'),
and then run php artisan iseed so it will get all the tables from your existing db and start creating seeders for all tables

Yii2 :: How to change existing enum list?

I have in my database enum list ('name_1','name_2','name_3'). Also I have create Model and Controller.
Later I have change enum list to ('new_name_1','new_name_2','new_name_3').
But in form I still see old list ('name_1','name_2','name_3').
What must I do to refresh enum list?
Thank you.
You should use Alter Table Modify
ALTER TABLE your_db.your_table
MODIFY Your_enum_col ENUM('new_name_1','new_name_2','new_name_3');
Rather simple. As #scaisEdge commented, you need to execute an update query.
On a migration you can execute the following code to edit an ENUM column on Yii2.
public function safeUp()
{
$query = "ALTER TABLE TABLENAME MODIFY tipo ENUM('VALUE1','VALUE2','VALUE4','VALUE5') default 'VALUE1'";
$this->execute($query);
}
In this case we are also giving the default value of VALUE1, in case that we don't provide a value.
Remember that you can create the migration from the console using the yii command.
./yii migrate/create add_enum_value_to_table
The migration should be on the console folder, under migrations.
Hope that it helps !

EF and Code First Migrations with MySQL - dbo.tablename does not exist

I have set up entity framework to use MySQL and created a migration.
When I run update-database I get error "table dbname.dbo.tablename' does not exist. Running in -Verbose mode I see the statement that causes the error :
alter table `dbo.Comments` drop foreign key `FK_dbo.Comments_dbo.Comments_Comment_ID`
When I run the query direct in MySQL workbench is throws the same error.
The problem seems to be the dbo. prefix in the migration set. Anything in the form dbo.tablename won't run saying that table does not exist. E.g. select * from dbo.tablename fails but select * from tablename works. The database was generated by Entity Framework and the code first migrations were generated by EF too.
However the migrations generate everything with the dbo. prefix, which does not work.
Does anyone have a solution to this?
I was having this problem just today as well; found my answer here:
MySqlMigrationCodeGenerator
You have to set:
CodeGenerator = new MySqlMigrationCodeGenerator();
In your context's configuration class. This will get rid of the schema gibberish for MySQL. My Configuration class looks like this:
internal sealed class Configuration : DbMigrationsConfiguration<YourContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
SetHistoryContextFactory("MySql.Data.MySqlClient", (conn, schema) => new MySqlHistoryContext(conn, schema));
CodeGenerator = new MySqlMigrationCodeGenerator();
}
}
I have this same issue and I applied the solution posted by Sorio and It generated a lot of problems.
CodeGenerator = new MySqlMigrationCodeGenerator();
This code line can change all the sql that you generate, for example in my case in the sql code to apply to the database all the foreign key constraints disappear.
I am still without a solution because for me this is not acceptable and I recommend you to check the sql generate with the command before use this solution:
update-database -script