Without referential integrity how does DBA create tables ?
And
How does DEV team raise Build release without Referential Integrity ?
Related
A developer I work with insists on maintaining a foreign key constraint in a database (MySQL) where the tables containing the related data are read only. How do I politely tell him this is unnecessary?
I wish to make a custom model, which has a foreign key relationship to the django_migrations table. Can this be done? if so, exactly how do I import the django migrations model?
Edit... Here is my plan
1: I have one database in which there are many tables on the 'Public' schema which are used for application-wide data. I suspect this system will get very large so for scalability I would like each of the clients to have their own schema. These schema will be dynamically generated upon creation of a client
2: The client schema are controlled by a separate python/django app which has it's own set of tables/migrations.
3: I want the system to be flexible so that in the future, when new features are created, I can deploy those features to a few specific clients without having to deploy to everyone therefore each of the client schema may be at a different migration level, hence, there is a need to track which migration level each client is at
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.
For testing database related code using JPA using an in-memory database such as HSQLDB:
How can I force an exception raised when executing EntityManager.joinTransaction() or EntityManager.getTransaction().commit()?
I'd like to force the exception without altering tables from the database (ex. deleting the table where the test would perform a persist()).
Some ways,
shutdown your database
violate a foreign key or unique constraint
For joinTransaction() I'm not sure you could do much, as it just joins the JTA transaction. It would throw an error if you were not in a JTA transaction, or potentially already in a transaction, or resource_local.
I just need a confirmation for my understanding on Django's implementation of ON DELETE CASCADE from you Django experts.
According to the official Django documentation:
When Django deletes an object, by default it emulates the behavior of
the SQL constraint ON DELETE CASCADE -- in other words, any objects
which had foreign keys pointing at the object to be deleted will be
deleted along with it.
Does the word "emulate" imply that the ON DELETE CASCADE logic is actually implemented in Django instead of at the database level? (I looked into my database and all the tables that contains foreign keys have ON DELETE NO ACTION in their definitions.)
If my understanding is correct, is there any way that I can relocate the ON DELETE CASCADE logic from the app layer to the database layer? I am more looking for a proper way, not an hack, of doing this. (Note: I am using PostgreSQL as my backend.)
If you are asking where the relevant code is implemented: You can find it here.
Implementing the CASCADE-DELETE logic in the application/ORM layer makes sense as this enables the app to get informed when deletion happens (eg. Django's delete signals are fired for the deleted instances), furthermore it is a sane way to enable this feature across different types of databases.
If you are worried about the integrity of your data: Django still sets foreign key constraints if your database supports this (eg. check Postgresql). So your database won't let you delete any rows a foreign key is pointing to.
Try it yourself:
> DELETE FROM accounts_publisher WHERE id=5;
ERROR: update or delete on table "accounts_publisher" violates foreign key constraint "accounts_publisher_id_411559b18a178e73_fk_accounts_publisher_id" on table "accounts_membership"
DETAIL: Key (id)=(5) is still referenced from table "accounts_membership".
FYI, this has been configurable since django 1.3:
https://docs.djangoproject.com/en/1.6/ref/models/fields/#django.db.models.ForeignKey.on_delete
Basically you need to set on_delete to DO_NOTHING and add the cascade logic to the DB yourself:
DO_NOTHING: Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add a SQL ON DELETE constraint to the database field (perhaps using initial sql).