I renamed my table with the following command:
ALTER TABLE IF EXISTS wds RENAME TO userconfigs;
However running:
SELECT * FROM information_schema.table_constraints;
Returns entries called wds_pkey or wds_fkey.
How can I rename the table such that constraint names are also adapted to the new name (userconfigs_pkey, userconfigs_fkey)?
Postgresql version 10.16.1
Related
I got some initial migration, that creates some related tables.
When I found out tables in scheme are named incorrectly, I decided to rename them like this
After that i tried to upgrade database (conduit db upgrade), but got issue that says:
C:\flutter_projects\dart_backend>dart run conduit:conduit db upgrade
Building package executable...
Built conduit:conduit.
— Conduit CLI Version: 4.1.6
— Conduit project version: 4.1.6
— Updating to version 2 from version 1...
Replaying versions: 1...
PostgreSQL connecting, postgres#localhost:5432/flutter-project.
Applying migration version 2...
CREATE TABLE authors (id BIGSERIAL PRIMARY KEY)
CREATE TABLE posts (id BIGSERIAL PRIMARY KEY,content TEXT NOT NULL)
CREATE TABLE users (id BIGSERIAL PRIMARY KEY,userName TEXT NOT NULL UNIQUE,email TEXT NOT NULL UNIQUE,password TEXT NOT NULL,accessToken TEXT NULL,refreshToken TEXT NULL)
CREATE INDEX users_userName_idx ON users (userName)
CREATE INDEX users_email_idx ON users (email)
ALTER TABLE posts ADD COLUMN author_id BIGINT NOT NULL
CREATE INDEX posts_author_id_idx ON posts (author_id)
ALTER TABLE ONLY posts ADD FOREIGN KEY (author_id) REFERENCES authors (id) ON DELETE CASCADE
DROP TABLE _Author
*** There was an issue. Reason: удалить объект таблица _author нельзÑ, так как от него завиÑÑÑ‚ другие объекты. Table: null Column: null
How can i solve this problem? Is it possible to delete tables cascade? Or can I force conduit rename instead of delete-create?
Solution was to edit migration and set correct order of table drops to exclude missing relations
database.deleteTable("_User");
database.deleteTable("_Post");
database.deleteTable("_Author");
I'm using psql and I want to change one of the columns of my table.
At the moment this column is a foreign key of Table 2 but I would like to make it point to Table 3.
Is this possible or should I delete the column and add a new one?
There's no need to add & remove the column. You can remove/disable the constraint to one table and add it for the other table.
The command for doing the former is:
alter table Table1 drop constraint if exists name_of_constraint_on_Table_1_column
The command for doing the latter is:
alter table Table1
add constraint name_of_constraint_on_Table_1_column
foreign key (column) references Table3 (other_column) match full
You need to find the name of the foreign key constraint if you haven't named it explicitly. You can do so via the \d command:
\d Table1
You should read about alter table cause there's a lot of things you can do to change the table.
Scenario
A table in SQL Server has two or more columns, but the original column with the primary key constraint is no longer needed. So now you want to write a script to drop the original column w/ a PK constraint and put the PK constraint on a different column.
In this example, the table is empty.
Problem
You can't drop the first column without first dropping the PK constraint.
And you can't drop the PK constraint in SQL Server without the exact name of it. (more info here)
....But you don't know the automatically generated name of the PK constraint.
NOTE: If the table is not empty, see this solution:
SQL Server 2008 Script to Drop PK Constraint that has a System Generated Name
(In most cases, this is the best solution.)
Question
The above solution will work, but what is another way to script dropping a column with a PK constraint when you don't know the constraint's name in an empty table?
Another strategy -- besides figuring out the system generated name of the PK constraint so you can drop it as described here -- is to drop the empty table and recreate it without the original column with the primary key constraint, instead putting it on the new column.
To drop the column with an unknown PK constraint name:
Generate a script to drop the table and re-create it from scratch
Remove the OriginalColumn column from the CREATE TABLE query
Put the PK constraint on the NewColumn column in the script
Run the script to drop and re-create it without the original column -- effectively dropping OriginalColumn and "moving" the PK constraint from OriginalColumn to NewColumn
???
Profit!
I'm adding delete cascading to a Table. The Clone table has a column DeviceID that is a foreign key to the Device table's DeviceID column. So the SQL script drops the original FK constraint, and attempts to add the new one:
IF EXISTS
(
SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_NAME = 'FK_Clone_Device'
)
BEGIN
ALTER TABLE Clone
DROP CONSTRAINT FK_Clone_Device
END
IF NOT EXISTS
(
SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_NAME = 'FK_Clone_Device_Cascade'
)
BEGIN
ALTER TABLE Clone
ADD CONSTRAINT FK_Clone_Device_Cascade
FOREIGN KEY (DeviceID) REFERENCES Device(DeviceID) ON DELETE CASCADE
END
When I run this script, I get the following error:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Clone_Device_Cascade". The conflict occurred in database "DevelopmentDB", table "dbo.Device", column 'DeviceID'.
Maybe I'm misunderstanding the error message, but it sounds like it's conflicting with itself. I'm confused that it happened on the Device table though.
There is an index in the Clone table on DeviceID. Would that matter?
This is on SQL SERVER R2 (Azure)
Sounds like you currently have data in the table that would violate the FK you are trying to create. One way to test this is to add "WITH (NOCHECK)" to the ALTER TABLE statement and see if it lets you create the constraint.
If it does let you create the constraint with NOCHECK, you can either leave it that way and the constraint will only be used to test future inserts/updates, or you can investigate your data to fix the FK violations.
So your example would be:
ALTER TABLE Clone WITH NOCHECK
ADD CONSTRAINT FK_Clone_Device_Cascade
FOREIGN KEY (DeviceID) REFERENCES Device(DeviceID) ON DELETE CASCADE
I want to create table with to columns:
IdRole
IdProcedure
the idea is that IdProcedure is a FK to sys.objects.
When I create this query:
SELECT *
FROM sys.objects
WHERE type='p'
it works fine, but this one:
ALTER TABLE dbo.CORE_ProcedureXRole
ADD CONSTRAINT FK_SysProcedure
FOREIGN KEY (IdProcedure)
REFERENCES sys.objects(object_id)
tells me:
Foreign key 'FK_SysProcedure' references invalid table
'sys.objects'.
sys.objects isn't a table. It's a system view backed by data stored in proprietary SQL Server format. If you want to make sure that the stored name is correct, add a TRIGGER for update and insert to handle the checking.