Why are primary keys and unique constraints disappearing from tables? - django

We are using Django with PostgreSQL on Heroku. We have tables with primary keys and unique constraints (created with class Meta / unique_together in Django). But the problem is, we found at least a few times that tables don't have primary keys and unique constraints - they disappeared for unknown reasons.
Does anyone know why the constraints disappeared? We didn't get any error messages but there were duplicate values in the tables where the unique constraints should have prevented them. The primary keys and unique constraints just disappeared.
What happened and how do we prevent it from happening again?

Related

Django Postgres Database Migration

I have an access database with two tables, members and residential areas, I'm using a tool to migrate the tables from access to postgres on railway.app. The problem is, when i run migrations from django, i get a relationship doesn't exist error. (I have a foreign key attribute in members referencing residential areas because each member is linked to a residential area.
I understand this is happening because when the data gets to postgres, it doesn't have a unique ID fields(the auto-generated ID from django), I've tried using custom fields as primary keys but nothing seems to work. Anyone with an idea how I can solve this issue?

How many foreign keys can a table have in postgreSQL?

I am working on a Django project with a database in PostgreSQL. During my schema design, I have noticed that one table is referencing many foreign keys from other tables. Just curious that how many foreign keys can be referenced from/to one table? I also searched and found that the SQL server 2014 can reference up to253 foreign keys.
I don't think there is a hard limit. I just created a table with 10001 foreign key constraints.
Of course that doesn't mean that it is sane to define that many foreign keys, and performance will suffer considerably.

Duplicate value in column error - Azure Analysis Services

I have a table in my model which is extracting data from an adls location in Azure Datalake.
I am getting the following error while deploying the model:
'Column 'xy_id' in Table 'ABC' contains a duplicate value '' and this is not allowed for columns on the one side of a many-to-one relationship or for columns that are used as the primary key of a table.
I have checked the adls file for duplicates. There are no duplicate values. Also checked the count and distinct count in AAS which is same.
All the tables are getting processed successfully. The error comes on the "Deploy Metadata" step and the deployment fails.
There are only 3 tables in the model. I have created a One to Many relationship from table ABC to other 2 tables.
Can anyone suggest any fixes? I am not able to figure out why I am facing this error.
Thanks in advance.
You are interacting with different systems which stores data in different ways. By default Azure Analysis Services is case INsensitive.
This means that AAA and aAa are the exact same thing. Therefore, it will cause issues if:
the field is defined as Unique in AAS
the field is defined as Primary Key in AAS
the field is on the one-side of a one-to-many relationship
You can either solve this issue in the data source or change the database settings in SSMS.
However, as a general best-practice please define relationship only on integer fields. For example, you could create a Surrogate Key in the data source.

Django OneToOneField on foreign table

I'm trying to setup a new server with foreign tables (using postgres_fdw) that weren't foreign tables previously, and I have some OneToOneFields pointing to these tables. This doesn't work out of the box - OneToOneFields use foreign keys, and postgres_fdw does not support foreign keys for foreign tables.
The foreign tables are in a read-only database on the same server.
Is there an easy way to get this working?
After a little more research, I think the answer is 'don't do that'. Handling foreign keys for foreign tables isn't implemented, and any solution that tried to bridge that gap would have serious performance and/or consistency issues.
pglogical looks like a better solution - instead of pulling the data in through postgres_fdw, replicate the tables into the new database so they're local. There are other projects for replicating just specific tables, but pglogical is included with PostgreSQL 10.

one primary key column foreign key to 2 other table columns.How to resolve data entry issue

I have a requirement according to which I have to create a central Login system.We have 2 things Corporate and Brand each represented by tables "Corporate" and "Brand".
When a corporate gets registered,corporateID is given,When a user under that corporate gets registered there is a table corporateuser in which corporateID is a foreign key and CorporateUserID is a primary key.Similarly in the case of a brand.
So we have CorporateUserId and BrandUserID.
Now i have a table called RegisteredUsers in which i want to have corporate as well as brand users.UserID is a primary key in this table which is a foreign key to both corporateuser as well as Branduser.
now when i enter a corporateuser,I do an entry to corporateuser as well as RegisteredUsers.When i enter CorporateUserID in userID for RegisteredUsers.It gives foreign key violation error.
I fully understand this error.How can i achieve this.This requirement is very rigid.Please tell a workaround
What you're trying to do is not totally clear, but it seems that you want the primary key of all three user tables to be the same. This is not a strict foreign key relationship, but it seems reasonable in your application.
You need to assign the userID in RegisteredUsers first, and use that key when you create your Corporate User or Brand User. Then the user id's will be unique across the whole system.
If that's not what you want, edit your entry with the table layouts to make the problem clearer.
If you are trying to insert records into tables with relational conatraints, you will need do all inserts under one SQL Transaction.