I am running migrations on my production system which uses a Postgress database and when I run it I get this error:
django.db.utils.ProgrammingError: multiple primary keys for table "website_experience" are not allowed
But works well on my development SQL database. Here's the model I'm working with:
class Experience (models.Model):
title = models.CharField(max_length = 60)
company = models.CharField(max_length = 60)
city = models.CharField(max_length = 60)
start_date = models.DateField(blank=False, default=datetime.now)
end_date = models.DateField(blank=True, null=True)
description = models.CharField(max_length = 1000)
creative_user = ForeignKey(CreativeUserProfile, models.CASCADE)
Initially, the field creative_user (which is my extended User model) was a primary key, but changed it to be a ForeignKey to express One to Many relationship between One CreativeUser having Many work Experience.
Here is the migration before and after making the change to ForeignKey
class Migration(migrations.Migration):
dependencies = [
('website', '0003_auto_20170510_1436'),
]
operations = [
migrations.CreateModel(
name='Experience',
fields=[
('title', models.CharField(max_length=60)),
('company', models.CharField(max_length=60)),
('city', models.CharField(max_length=60)),
('startDate', models.DateField()),
('endDate', models.DateField(blank=True, null=True)),
('creative_user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='website.CreativeUserProfile')),
],
),
]
This expresses the creation of Experience model and that creative_user was primary key on model. Then after making it a ForeignKey the migration looked like:
class Migration(migrations.Migration):
dependencies = [
('website', '0004_experience'),
]
operations = [
migrations.AddField(
model_name='experience',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
preserve_default=False,
),
migrations.AlterField(
model_name='experience',
name='creative_user',
field =models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='website.CreativeUserProfile'),
),
]
As I said this all works on dev but migrating on Postgress DB thinks I have multiple primary keys. Can anyone shine some light on what wrong I'm doing?
Thanks.
Maybe is a issue related to the order of migration changes. I had this in my migration file:
operations = [
migrations.AddField(
model_name='unsubscriber',
name='id',
field=models.AutoField(default=None, primary_key=True, serialize=False),
preserve_default=False,
),
migrations.AlterField(
model_name='unsubscriber',
name='phone',
field=models.IntegerField(verbose_name='Teléfono'),
),
]
In the example I wanted to change the primary_key from phone to the new field called id, as you can see this migration is trying to create the new field as PK without changing the old one.
Just changing the order to this must work:
operations = [
migrations.AlterField(
model_name='unsubscriber',
name='phone',
field=models.IntegerField(verbose_name='Teléfono'),
),
migrations.AddField(
model_name='unsubscriber',
name='id',
field=models.AutoField(default=None, primary_key=True, serialize=False),
preserve_default=False,
),
]
It solves the problem.
I hope it helps.
I had the same issue and managed to resolve it by deleting all the migration files from the point the affected table was created, and then run makemigrations and migrate.
Your migration file "0004_experience" created a oneToOneField named "creative_user" that was set as the primary key.
My guess is, Changing from a onToOne to oneToMany relationship called for creation of a new unique field (an auto increment field "id" and set it as the primary key) in the later migration, because "creative_user" was nolonger unique.
Since the latest migration depends on migrations before it, you ended up with two primary keys.
Deleting these conflicting migration files will sort you out.
I have deleted all the migration files except init.py and run migration commands again.
python manage.py makemigrations
python manage.py migrate appName
which solved my problem.
Related
I decide to add new Model called Project to my project:
When I run python manage.py migrate, it shows me the below error:
class Project(models.Model):
statut_juridique=[
('per', 'personne physique' ),
('sarl', 'SARL'),
('sual', 'SUARL'),
('anony', 'SA'),
]
type_du_projet = [
('ind', 'industrie'),
('agr', 'agronome'),
('ser', 'service'),
('art', 'artisanat'),
('com', 'commerce'),
]
name = models.CharField(max_length=50)
produit = ArrayField(
ArrayField(
models.CharField(max_length=20, blank=True),
size=8,
),
size=8,
)
stat_jur = models.CharField(max_length=50, choices=statut_juridique)
type_projet = models.CharField(max_length=50, choices=type_du_projet)
Nomination = models.CharField(max_length=50)
adresse = models.CharField(max_length=200)
user = models.ForeignKey(User, related_name='projet', on_delete=models.CASCADE)
def __str__(self):
return self.name
Operations to perform: Apply all migrations: admin, auth,
businesplan, contenttypes, sessions Running migrations: Applying
contenttypes.0001_initial...Traceback (most recent call last): File
"/home/abdallah/projectdjango/oasis/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
line 87, in _execute
return self.cursor.execute(sql) psycopg2.errors.DuplicateTable: relation "django_content_type" already exists
And also I can't see the new table in my Database, Can you help me please!
You probably use a database that already has some tables with their migrations.
For this case you can Try using a new database, or reset your existing database to remove duplicate tables or sometimes you can troubleshot using --fake-initial as one of django-admin cmd option:
$ python manage.py migrate --fake-initial
From Django-Doc:
Allows Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names and so is only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.
Source: django-admin#cmdoption-migrate-fake-initial [django-doc]
I have the below model which is an existing DB model and through Django's inspectdb management command the below model is created.
class ExistingLegacyModel(models.Model):
period = models.TextField(db_column="Period", blank=True, null=True)
key = models.AutoField(db_column="OutlookKey", primary_key=True)
class Meta:
managed = False
db_table = "table_name"
and currently, I'm trying to create a model with a field foreign key reference to the existing legacy DB model
class TestModel(models.Model):
period = models.ForeignKey(
ExistingLegacyModel,
on_delete=models.CASCADE,
db_column="OutlookKey",
)
so when I run the makemigrations command the migration file is successfully getting created with no issue. below is the migration file content.
class Migration(migrations.Migration):
initial = True
dependencies = [
('historical', '0011_phoenixcontractprice'),
]
operations = [
migrations.CreateModel(
name='TestModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('period', models.ForeignKey(db_column='OutlookKey', on_delete=django.db.models.deletion.CASCADE, to='app.ExistingLegacyModel')),
],
),
]
so now when i run the migrate command now, it is failing and giving the below error.
django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Column 'table_name.OutlookKey' is not the same data type as referencing column 'version_testmodel.OutlookKey' in foreign key 'version_testmodel_OutlookKey_eb16c31c_fk_table_name_OutlookKey'. (1778) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Could not create constraint or index. See previous errors. (1750)")
I'm stuck with this issue for the past couple of days and I searched all over the internet but didn't get any resolution. I found a couple of StackOverflow questions that are very similar to my issue, but those questions are also unanswered.
Django - Migration foreign key field type not matching current type
Django 3.2 update AutoField to BigAutoField backward compatibility with foreign key relations
I'm currently using Django 3.2.13 and mssql-django to connect to the MSSQL database.
Any help on this will be highly appreciated! Thank you in advance.
UPDATE 1
I ran the sqlmigrate command for the initial migration. So for the period column, it is creating the table with a foreign key field with big int [OutlookKey] bigint NOT NULL whereas the existing legacy model has a normal integer field.
ALTER TABLE [<app>_<model>] ADD CONSTRAINT [<app>_<model>_OutlookKey_3505d410_fk_<existing_legacy_table>_OutlookKey] FOREIGN KEY ([OutlookKey]) REFERENCES [<existing_legacy_table>] ([OutlookKey]);
I have been struggling to get the final SAMPLE (ASP.Net, EF Core, SQL) to work against a real SQL Server. Every sample I can find does not use real SQL they always opt for in-memory data store
I changed the connection string
"Data Source=.;Initial Catalog=IS4;Integrated Security=True;"
and ran
dotnet ef database update -c ApplicationDbContext
This created me a SQL database with 25 tables.
I tweaked Startup.cs to change
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
and b.UseSqlite to b.UseSqlServer
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = b =>
b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
// this adds the operational data from DB (codes, tokens, consents)
.AddOperationalStore(options =>
{
options.ConfigureDbContext = b =>
b.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
// this enables automatic token cleanup. this is optional.
options.EnableTokenCleanup = true;
// options.TokenCleanupInterval = 15;
});
I ran the server with "/seed" on the command line but the Seed functionality doesn't work
First it complains CLIENT can't have a NULL ID when it calls SaveChanges(). If I change the code to add the ID
if (!context.Clients.Any())
{
Console.WriteLine("Clients being populated");
int i = 1;
foreach (var client in Config.GetClients().ToList())
{
var x = client.ToEntity();
x.Id = i++;
context.Clients.Add(x);
}
context.SaveChanges();
}
else
{
Console.WriteLine("Clients already populated");
}
I then get
"Cannot insert the value NULL into column 'Id', table 'IS4.dbo.ClientGrantTypes".
When I watch the video's it says it can be migrated from SQLite to full SQL simply by changing the connection string which is obviously not true, given all the other changes I have done, so I must be doing (or missing) something else.
Any thoughts?
Could it be that all the tables with an "Id INT" column should all be IDENTITY columns and they are not!
I checked the migrations code and it has
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ApiResources",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Description = table.Column<string>(maxLength: 1000, nullable: true),
DisplayName = table.Column<string>(maxLength: 200, nullable: true),
I am guessing
.Annotation("Sqlite:Autoincrement", true),
doesn't work with full SQL and therefore all the tables need identity properties setting.
Interestingly if you run the other template to add the AdminUI
dotnet new is4admin
It seems to add a couple of SQL scripts
CREATE TABLE "Clients" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_Clients" PRIMARY KEY AUTOINCREMENT,
"AbsoluteRefreshTokenLifetime" INTEGER NOT NULL,
"AccessTokenLifetime" INTEGER NOT NULL,
which does make them identity columns.
I was faced with this issue today and did a couple of searches online and stumbled upon this https://entityframeworkcore.com/knowledge-base/46587067/ef-core---do-sqlserver-migrations-apply-to-sqlite-
The link pointed out to switch the annotation portion in the migration class UP method after
Id = table.Column(nullable: false)
from
.Annotation("Sqlite:Autoincrement", true);
to
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
And you will need to import
using Microsoft.EntityFrameworkCore.Metadata;
Then you build, and the migration will be successful.
To resolve this particular issue I used SSMS.
right click on table
select script to drop and create
add IDENTITY after the NOT NULL
Execute
However you are correct, it is using sqlite annotations in the sql file and in the migrations.
To fully resolve this issue, you need to create an implementation of all 3 necessary database contexts: identity, persisted grant, and configuration.
That requires an implementation of design time factories for each of those contexts as well.
Then you can run add-migration in the package manager console for each of those contexts, and then run update database, or run the application with the migrate function when seeding.
So to recap:
Create implementations for the 3 db contexts
Create Design time factory implementations for those db contexts
Add the migrations
Update the database with those migrations
I am writing migrations script in cakephp 3 using Phinx.
I need to add a column with primary key while updating the table(using update() command) using migrations.
But when I run the script, it created the column but does not include the primary key.
$table->addColumn('book_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true
])->addPrimaryKey('book_id');
$table->update();
Thanks
Per the Docs:
Dealing with primary key can only be done on table creation
operations. This is due to limitations for some database servers the
plugin supports.
public function change(): void
{
$this->table('table_name')
->changePrimaryKey(['column1', 'column2'])
->save();
}
I have a django 1.8 project and I use so many models. I decide to create a new column in an specific model but it doesn't work because "the column 'x' doesn't exists". When I try the same in other model it works fine, is just in that model. I could drop that model and create it again, but I need to fix it without erase anything. Any idea?
column = models.CharField(max_length=50, null=True)
python3 manage.py makemigrations
... django.db.utils.ProgrammingError: column table.column doesn't exists
Finally I could update my model by editing the migration.py file like:
class Migration(migrations.Migration):
dependencies = [
('appName', '000X_auto_...'),
]
operations = [
migrations.AddField(
model_name='modelName',
name='column',
field=models.CharField(max_length=50, null=True),
),