How to Clone selected constraints in sqlalchemy table? - postgresql

Inspired by creating a temporary table from a query using sqlalchemy orm I had similar requirement but only needs PrimaryKey Constraint and want to add ForeinKeyConstraint based on new temp tables created.
I have added only foreign constraint by checking if the constraint is of type PrimaryKeyConstraint then add in constraints
constraints = [c.copy() for c in table.constraints if isinstance(c,PrimaryKeyConstraint)]
This is fine but I want to create a foreign key constraint on two newly created tables.
I have tried
constraints.append(ForeignKeyConstraint(['id'],['schema2.temp_table1.id']))
and as suggested added back to the table
temp_table = Table(name, metadata, *(cols + constraints), schema=schema)
But it is taking a long time to create and insert the data. Any suggestions would be helpful.

Related

Query which will delete other table reference as well as from main table

I have a requirement of deleting records from the Postgres database tables.
We have a Customer table which is the main table, this table contains a primary key which is used in so many other tables as a FOREIGN KEY, I want to delete one of the customers as well as its reference used in other tables. Is there any way to delete the customer from main table as well as from other tables which contains foreign key.
Thanks in Advance.
In the other tables, you want a cascading delete foreign key reference. You can create one in the database using:
alter table othertable add constraint fk_othertable_customerid
foreign key (customerid) references customers(customerid)
on delete cascade;
Note: This assumes that customerid is the name of the column in both tables and that it is already defined in the other tables.
A cascading foreign key constraint does exactly what you specify. When a row is deleted in the reference table, then all related rows are deleted.
If you already have foreign key constraints on customerid, then drop the existing constraint and add the cascading version.

updating table with Postgresql Foreign data wrapper

I created a foreign data wrapper table named t_user into mySchema.
IMPORT FOREIGN SCHEMA public LIMIT TO (t_user)
FROM SERVER myServer INTO mySchema;
The myServer side t_user added some column, but the foreign table didn't update accordingly.
I tried to delete the foreign table t_user, but it was used by my view and materialized view t_user, so the deletion failed.
Any ideas on how to update this table?
As you have seen, the foreign table definition does not change when the underlying table changes.
If all you did is add a column, you can use ALTER FOREIGN TABLE to add a corresponding column to the foreign table. That should work even if views depend on the foreign table.
For example, if the column is of type text, you can do:
ALTER FOREIGN TABLE t_user ADD COLUMN my_column text;

How would you create a unique index on a foreign table?

Database 1 has foreign tables a and b on database 2.
How can we create indexes on these foreign tables a and b. These foreign tables are wrappers over database2.c and database2.d tables respectively, which do have the necessary indexes in place.
How would you create indexes on foreign tables a and b? Is that even possible?
I get a cannot create index on foreign table a - when I try a simple Create Index command in postgres
You cannot create index on a foreign table, instead write a trigger on foreign table and create a local table in postgres such that whenever an insert, update or delete is happening in your foreign table it will be reflected in your local table and index it.
Joining with a foreign table can result to slow query's.
Since indexes are not a option with foreign table, consider making a materialized view on a foreign table . Materialized views do allow indexing
CREATE FOREIGN TABLE members_fdw(...)
CREATE MATERIALIZED VIEW members AS
select * from members_fdw
WITH DATA
CREATE UNIQUE INDEX "member_id" ON members USING btree ("id");

Altering table for adding foreign key constraint take very long time

I have one table manual_errors_archive. I need to add foreign key to this table keeping reference to values table, values table having 800,000 records and manual_errors_archive table does not have any records.
ALTER TABLE manual_errors_archive
ADD CONSTRAINT fk_manua_reference_value
FOREIGN KEY (value_id)
REFERENCES values;
Postgres version i am using 9.1
But this is running for more than 1 hr after that I canceled the process. Any idea how to optimize this process?

How to add Primary Key from Existing table in SQLite?

Now i am working in SQLite database. I want to add primary key from my existing table. But i couldn't add the primary key in existing table and i have deleted all the records in the old table. So how can i add the primary key from existing table in SQLite?
Here my query is,
alter table studetails add constraint pk primary key (rollno).
I have used that above query, but it shown error. Is it possible?, if yes, please guide me.
Thanks!
you cannot according to SQLite's syntax perform this operation. In general it seems strange to add a primary key to an existing table. This should be done using the create table statement (you can also add an index by create index).
Br
Anders
Alter table in SQLite
Create table in SQLite