laravel 5.5 migrations not creating migration records - database

when I run php artisan migrate I get two tables users and migrations. but when I run the same command again to add new table. I get the following error,
`PDOException::("SQLSTATE[42S01]: Base table or view already excists: 1050
Table 'users' already exists")`
when I checked the migrations table it is empty. I have not manually deleted anything from the database or even deleted any migration files in laravel.
what is causing this, and how to solve it.

Related

ASP.NET Core 3.0 - Can't Update-Database

I just created the project using the command dotnet new angular -o <output_directory_name> -au Individual and scaffold identity then I installed Microsoft.EntityFrameworkCore.SqlServer but when I run the command update-database, I get the error below.
Failed executing DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AspNetRoles] (
[Id] TEXT NOT NULL,
[Name] TEXT(256) NULL,
[NormalizedName] TEXT(256) NULL,
[ConcurrencyStamp] TEXT NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);
and then at the end end another error
Error Number:2716,State:1,Class:16
Column, parameter, or variable #2: Cannot specify a column width on data type text.
Below is the generated CreateIdentitySchema migration
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<string>(nullable: false),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
});
ApplicationDbContextModelSnapshot.cs
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken()
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.HasColumnType("nvarchar(256)")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasColumnType("nvarchar(256)")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex")
.HasFilter("[NormalizedName] IS NOT NULL");
b.ToTable("AspNetRoles");
});
How to fix this errors so I can update-database?
Changing 'TEXT' to 'VARCHAR' in 00000000000000_CreateIdentitySchema.Designer.cs resulted in the following error. The same happens when I change it to 'NVARCHAR'
Data type 'VARCHAR' for property 'Id' is not supported in this form. Either specify the length explicitly in the type name, for example as 'NVARCHAR(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type.
I had this error and it can be fixed as follows.
Open up the project and delete 2 migration files and 1 snapshot file. Most likely named 0000...InitialCreate.cs, 0000...InitialCreate.Designer.cs and the ...DbContextModelSnapshot.cs. Leave the DbContextClass.
Delete the database.
Use dotnet to create a new migration and update the database. I also script the migration to check it.
dotnet ef migrations add InitialCreate --context ApplicationDbContext -v
dotnet ef database update --context ApplicationDbContext -v
dotnet ef migrations script --context ApplicationDbContext -v
These answers were all helpful, especially the one from #klent. I had generated a new project using:
dotnet new angular -o [MY PROJECT] -au Individual
This produced a new project that used Sqlite as the data provider, although I wasn't aware of that. I changed the connection string to my SQL Server, then ran:
dotnet ef database update
and got the error posted in the original question. After reviewing the answers here I realized that I had the incorrect data provider, and the migrations were targeting Sqlite syntax, which was using TEXT(256) for one of the column specifications. So my specific steps to correct were to:
Remove the Microsoft.EntityFrameworkCore.Sqlite Nuget package.
Install the Microsoft.EntityFrameworkCore.SqlServer Nuget package.
In the ConfigureServices method of my Startup.cs, change the services.AddDbContext call to use options.UseSqlServer instead of options.UseSqlite.
Remove the _CreateIdentitySchema and ApplicationDbContentsModelSnapshot classes from the Migrations folder.
After ensuring the connection string in AppSettings.json points to the SQL Server and not Sqlite, run dotnet ef migrations add CreateIdentitySchema -o Data\Migrations. Note that the output folder specified in the -o parameter may be different for you.
Check the Visual Studio Project Explorer. Two new classes should now appear in the Migrations folder.
Run dotnet ef database update. This will create the tables in the database noted in the connection string.
Additionally, if you want your tables to be in a specific schema, for example "Auth", open your ApplicationDbContext class and add:
protected override void OnModelCreating( ModelBuilder builder )
{
base.OnModelCreating( builder );
builder.HasDefaultSchema( "Auth" );
}
However, this will set the schema for all access that uses this DbContext. It wasn't a problem for me since I'm not using EF for my application data, just Identity data. YMMV.
As already answered by a few others the default Identity installation seems to create the wrong column types; namely that some of the Id columns should be varchar and not text.
I had exactly the same issues as you and spent ages trying to effectively solve it.
The quickest way I found to solve it, and for anyone else coming across this problem, is to:
Create the new project
Delete the Migrations folder completely
Install the package for Sql Server (Or whatever data system you're using) and connection string details
Add your own migration using dotnet ef migrations add <MIGRATION NAME> or Add-Migration (this will write new migration designer files from scratch with the correct column structure
Lastly, update your database with dotnet ef database update or Update-Database
As per comments from #sepupic and #PeterSmith, the problem was that the auto-generated code in 00000000000000_CreateIdentitySchema.Designer.cs had TEXT fields with HasMaxLength so I changed it to VARCHAR then I added HasMaxLength(450) to all VARCHAR Ids then I tried to update-database again and it worked.
I run the project and tried to Register a user and I got the error below
System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Bolean'
Since I still had errors I did the following:
I deleted the generated table then created a new project with the command dotnet new angular -o <output_directory_name> -au Individual
I didn't scaffold new identity to the newly created project
I copied the db name from my previous project and run the command update-database
There were two missing tables DeviceCodes and PersistedGrants so I run the command add-migration but it didn't generate anything so I copied the migration code of the two tables from my previous project and pasted it the empty migration code I created earlier.
I run the project again and register a new user and it finally worked
I still don't know why I got errors since the project I did before didn't have any problem with the auto-generated code.
I had an error like yours as
Column 'Id' in table 'Roles' is of a type that is invalid for use as a key column in an index.
Because I changed the table names those are coming with Individual Identity of Asp.Net Core web application.
To put it simply,
Delete everything in Migrations Folder apart from ApplicationDBContext.
Add-Migration
Make sure if your tables names are matching in Up, Down and BuildTargetModel
Then update-database
It worked for me!
Edit : My main point is that make sure if your tables names are matching in Up, Down and BuildTargetModel. After that update-database will work.
I resolved this database update error with the first answer by #abCsharp, but not until I noticed that the project, which I started with the option of Identity, had created a new migrations folder under the project folder. The original migrations folder was under a Data folder that also contained the ApplicationDbContext.
I deleted the original folder (for cleanliness) and ran the newly created migration which worked. I also deleted the migrations history table in the database, which contains tables for which I am not using migrations.
FWIW All the data types for text in all the tables are nvarchar.

Missing Migration data in SQL when trying to add-migration

I have made use of a backup database and have added a new table using Code First. Now when I tried using add-migration, I got this error:
Unable to generate an explicit migration because the following
explicit migrations are pending
Suggesting that the Migrations in the project do not match which are in the DB, which when I looked, there are NO migrations in the __migration table any more ?
As these tables exist in the DB, how do I get all of these migrations inserted into my SQL DB ? Is there a way I can get them all added, so that the projects migrations and the SQL DB migrations will match, so that I can continue adding/editing tables ?
Thanks in advance for any help.
So I found something that did help! It unfortunately meant that I lost all of my migration files, but thats not the end of the world.
The below is reference from this site: Click here
Remove the _MigrationHistory table from the Database
Remove the individual migration files in your project's Migrations folder
Enable-Migrations in Package Manager Console
Add-migration Initial in PMC
Comment out the code inside of the Up method in the Initial Migration
Update-database in PMC (does nothing but creates Migration Entry)
Remove comments in the Initial method
I hope this helps someone else as well.

Update Database with Migrations CLI on VS for Mac (DotNet Core 2)

I created the Models of my database on VS for Mac and used terminal for create migrations:
dotnet ef migrations add IntitialMigration
Then, I updated my database:
dotnet ef database update
But after I changed my Models and I created another migration:
dotnet ef migrations add SecondMigration
And tried to update the database:
dotnet ef database update SecondMigration
I received the following error:
There is already an object named 'Emails' in the database.
I've searched and I found a probably solution:
Add-Migration SecondMigration -IgnoreChanges
But this only works at PMC on Windows. I'm using VS for Mac and all the commands are typed on Terminal. Does anybody know how to update a database using Migrations after change the Models?
I found the solution. It seems that on VS 2017 for Windows, when you add a Migration, it is automatically added on the solution. But, on VS for Mac, that doesn't happen. You need to manually add the each new migration to the solution. I created the InitialMigration migration and update it to the database. For some reason I don't know why, I removed the migration from the solution and I created the new SecondMigration migration. That's the reason that all the tables that were on InitialMigration were on SecondMigration too.
If you just run update database it will also try to update with previous migration. (which is why its complains about adding whats already there). You can run an update just for one single migration by adding the name of the migration you want to run after the command.

How can I store required 'base' or 'initial' data for a database (in particular Symfony)?

I use the doctrine migrations bundle to track changes in my database structure. I would like to ensure that when I'm deploying / adding a new server for my application that:
(A) the database schema is up to date (doctrine:migrations:migrate)
(B) the database always contains a pre-defined set of data
For (B) a good example is roles. I want a certain set of roles to always be present. I realize it is possible with database migrations, but I don't like the idea of mixing schema changes with data changes. Also if I use MySql migrations I would have to create a equivalent Sqlite migration for my test database.
Another option I'm aware of is data fixtures. However from reading the documentation I get the feeling that fixtures are more for loading test data. Also if I changed a role name I don't know how that would be updated using fixtures (since they either delete all data in the database before loading or append to it). If I use append then unique keys would also be a problem.
I'm considering creating some sort of command that takes a set of configuration files and ensures that certain tables are always in a consistent state matching the config files - but if another option exists I'd like to use it of course.
What is the best way to handle loading and managing required data into a database?
If you're using Doctrine Migrations, you can generate initial migration with whole database schema, then you should generate migrations (doctrine:migrations:generate or doctrine:migrations:diff) for all changes that are made in database structure AND also add there queries that will migrate existing data.
Fixtures are designed to pre-populate data (with doctrine:fixtures:load) and, in my opinion, they should be kept up-to-date with latest database schema and executed after doctrine:migrations:migrate / doctrine:schema:create.
So finally:
Create base migration with initial database schema (instead of executing doctrine:schema:create just generate migration file and migrate it)
Create new migrations for each database schema change AND for migrating existing data (such as role name changing)
Keep fixtures up-to-date with latest schema (you can use --append option and only update fixtures instead of deleting all database data first)
Then, when deploying new instance you can run doctrine:schema:create, then doctrine:migrations:version --add --all --no-interaction (mark all migrations as migrated, because you have already created latest schema) and doctrine:fixtures:load which will populate data to the database (also latest version, so data migrations from Doctrine migrations files are not required).
Note: Existing instances should NOT use doctrine:schema:update, but only doctrine:migrations:migrate. In our app we even block usage of this command, in app/console:
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Helper\FormatterHelper;
// Deny using doctrine:schema:update command
if(in_array(trim($input->getFirstArgument()), ['doctrine:schema:update'])) {
$formatter = new FormatterHelper();
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, true);
$formattedBlock = $formatter->formatBlock(['[[ WARNING! ]]', 'You should not use this command! Use doctrine:migrations:migrate instead!'], 'error', true);
$output->writeln($formattedBlock);
die();
}
This is what I figured out from my experience. Hope you will find it useful :-)

Reverse migration with South throwing error because table doesn't exist

I am trying to do a rollback with South in my django app. I am getting this message when I run my command: DatabaseError: table "forms_logo_version" does not exist. This is the command I'm running python manage.py migrate myformsapp 0044.
I've checked the DB and the table does not exist, but why is this a problem? If I am deleting the table anyway, why is this throwing an error? Is there anyway I force the script to continue?
I have simply created a blank table in the database and the script ran fine after that. I would still like to know the answer to my questions above.
You can find in your migrations file 0044_*.py method named backwards and remove line with something similar to db.delete_table('forms_logo_version'). If you do this makes some tests.
Connect to empty db and make forward migration and backwards, to be sure your changes don't affect another migration file.
I recommend you to figure it out why this table is missing from db? Probably South create this table (look into migrations) and only South should remove it.

Resources