SymmetricDS replication referencing wrong schema for foreign key - database

I use symmetricds replication for postgres database (one server node, one client node). I have got two schemas in my db: 'public' and 'schemaB'. Table from schemaB (tabB) has foreign key referencing field from table in public schema (tabA).
Table definition:
CREATE TABLE schemaB.tabB (
id character varying(255) NOT NULL,
external_id character varying(255),
)
ALTER TABLE ONLY schemaB.tabB
ADD CONSTRAINT fk_ma9cnn7779v065434iugg1321 FOREIGN KEY (external_id) REFERENCES public.tabA(id);
Symmetricds configuration for mentioned table:
insert into sym_trigger (trigger_id, source_schema_name, source_table_name, channel_id, sync_on_incoming_batch, last_update_time, create_time) values
('tid', 'schemaB', 'tabB', 'channel_B', 0, current_timestamp, current_timestamp);
During initial load for client node symmetricds tries to create table with definition:
ALTER TABLE "schemaB"."tabB"
ADD CONSTRAINT "fk_ma9cnn7779v065434iugg1321" FOREIGN KEY ("external_id") REFERENCES schemaB."tabA" ("id");
what causes error:
ERROR: relation "schemaB.users" does not exist. Failed to execute: ALTER TABLE "schemaB"."tabB"
ADD CONSTRAINT "fk_ma9cnn7779v065434iugg1321" FOREIGN KEY ("external_id") REFERENCES schemaB."tabA" ("id")
Symmetricds clearly references wrong schema. Can I do anything about it?

I would suggest having two symmetricDs engines for both schemas public and schemaB that would sync to corresponding schemas on the target node. To create another engine just duplicate the symmetric-ds.properties file, rename it to, let's say symmetric-ds-public.properties and edit to connect to the public schema.

Related

Why the table is not inserted in Model.edmx file diagram

I'm using ASP.NET MVC with SQL Server and EF6, when I created the ADO.NET data model, all my tables in the database were inserted into the .edmx diagram except this one table (shown below). When I opened the .edmx properties, I found this (MODEL.SupervisorAOI Assassination) but not (MODEL.SupervisorAOI Entity Type).
Is there any error in my table?
CREATE TABLE [dbo].[SupervisorAOI]
(
[supervisorid] VARCHAR (50) NOT NULL,
[AOIId] INT NOT NULL,
[SUPAOI] INT NOT NULL IDENTITY,
CONSTRAINT [FK_SupervisorAOI_AreaOfInterest]
FOREIGN KEY ([AOIId]) REFERENCES [dbo].[AreaOFInterest] ([AOIId]),
CONSTRAINT [FK_SupervisorAOI_Supervisors]
FOREIGN KEY ([supervisorid]) REFERENCES [dbo].[Supervisors] ([supervisorId])
);
The problem I could see is that your primary key needs not to have 'not null' as primary keys are not nullable by default. Make it look like the following.
[SUPAOID] INT IDENTITY
It is also advisable to name your primary key as ID or append the primary key with Id. Your primary key could be SUPAOID or SupervisorAOIID.
This table does not have a Primary Key. You may have SUPAOI column identity but this is not enough..
Working around EntityFramework need to have a Primary Key (Entity Key) on tables.The best practices each table should be PK. Change table structure and add a Primary Column then update Model..
[SUPAOI] INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
Note: if you do not use PK, you must provide table uniqueness , but it's not good idea..

Primary key no defined access database

I am developping an application in wpf. I have to work with an existing database in Access.
I used the ORM EntityFramework.
My problem is : in the database, its exists a table with no primary key so I can't add any values in this table.
The Error I get is: no primary key defined. I can change the definition of the table.
How can I solve my problem ? thx
So as to createy a primary key before you begin you must know that
a table can contain only one PRIMARY KEY constraint.
All columns defined within a PRIMARY KEY constraint must be defined as NOT NULL. If nullability is not specified, all columns participating in a PRIMARY KEY constraint have their nullability set to NOT NULL.
Security
Permissions
Creating a new table with a primary key requires CREATE TABLE permission in the database and ALTER permission on the schema in which the table is being created.
Creating a primary key in an existing table requires ALTER permission on the table.

Foreign key linked with primary key in same table

I have a table Categories with columns Id, ParentId (for "subcategories" whom can have any level of nesting) and some other. Using SQL Server 2012 I can't make foreign key in same table, FK_Categories_Categories (Id -> ParentId).
Error message is
'Categories' table
- Unable to create relationship 'FK_Categories_Categories'. The ALTER TABLE statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_Categories_Categories". The conflict occurred in database "pokupaykadb", table "dbo.Categories", column 'Id'.
That needs for cascade deletion of subcategories. What solution can be? It's desirable to be a some property, like cascade deletion from another table by foreign key
http://i.stack.imgur.com/kXiMS.png
If there are orphaned records that does not meet your constraint criteria - delete them before creating the foreign key.
Usually there are few records which doesn't go by the new constraint and that the DBMS doesn't allow to create the constraint.
In the case of orphaned values, the first occurrence is provided in the error label with the value that is orphaned.
It would certainly have helped to see what code you have tried to execute.
Below is a valid table definition :
CREATE TABLE dbo.Categories
(
Id int NOT NULL IDENTITY(-2147483648, 1)
CONSTRAINT PK_Categories PRIMARY KEY
, ParentId int NOT NULL
CONSTRAINT FK_Categories_ParentId
FOREIGN KEY (ParentId) REFERENCES dbo.Categories
)

How do I modify this SQL schema to constrain data in tables?

Newbie in need of help.
I'm creating a small database.
I want to constrain data in a couple of tables - Lender and Pingtree. The ProviderType table contains lookup data and contains either 'Lender' or 'Pingtree'. How can I modify this structure so that the Lender table only can contain Lender types and Pingtree, Pingtree types?
Guessing that ProviderTypeID is the column in the Provider table that distinguishes between the two types, then you must add this same column to both the Lender and Pingtree tables, add a suitable key (if it doesn't already exist) in Provider on ID, ProviderTypeId, and then add a composite FOREIGN KEY constraint to the Lender and Pingtree tables that include these columns.
While this may sound like a drag, it is a known pattern called supertyping/subtyping. When the supertype (Provider) can be multiple subtypes, you don't need the TypeId column. But when the subtypes are mutually exclusive, this is what you must do.
It might look something like this:
ALTER TABLE dbo.Lender ADD ProviderTypeId tinyint NOT NULL
CONSTRAINT DF_Lender_ProviderTypeID DEFAULT (1)
CONSTRAINT CK_Lender_ProviderTypeID_Is_Lender CHECK (ProviderTypeID = 1);
ALTER TABLE dbo.Pingtree ADD ProviderTypeId tinyint NOT NULL
CONSTRAINT DF_Pingtree_ProviderTypeID DEFAULT (2)
CONSTRAINT CK_Pingtree_ProviderTypeID_Is_Pingtree CHECK (ProviderTypeID = 2);
-- Any of a PK, UNIQUE constraint, or unique index will do
ALTER TABLE dbo.Provider ADD CONSTRAINT UQ_Provider_ID_ProviderTypeID
UNIQUE (ID, ProviderTypeID);
ALTER TABLE dbo.Lender DROP CONSTRAINT FK_Lender_ProviderId;
ALTER TABLE dbo.Lender ADD CONSTRAINT FK_Lender_ProviderId_ProviderTypeId
FOREIGN KEY (ID, ProviderTypeID) REFERENCES dbo.Provider (ID, ProviderTypeID);
ALTER TABLE dbo.PingTree DROP CONSTRAINT FK_PingTree_ProviderId;
ALTER TABLE dbo.PingTree ADD CONSTRAINT FK_PingTree_ProviderId_ProviderTypeId
FOREIGN KEY (ID, ProviderTypeID) REFERENCES dbo.Provider (ID, ProviderTypeID);
If written correctly (specifying the column list on INSERT) your stored procedures and application SQL code should not have to change.

Error upon creating tables in sqlplus

I'm new to sqlplus and was trying to run a sql script that creates a few tables, but once I try to run it, it gives me an error saying that the table or view doesnt exist and I dont know how to fix this error.
My script is:
drop table Borrower;
create table Borrower (
bid char(100) not null,
password char(100) not null,
name char(100) null,
address char(100) null,
phone char(100) null,
emailAddress char(100) null,
sinOrStNo char(100) null,
expiryDate date null,
--type ENUM('student','faculty','staff'),
type char(100) not null,
--CONSTRAINT Btype_check CHECK (type IN ('student','faculty','staff')),
FOREIGN KEY (type) references BorrowerType(type),
PRIMARY KEY (bid));
grant select on Borrower to public;
"unique/primary keys in table referenced by foreign keys "
Data integrity is crucial to a properly run database so Oracle will not let us drop a table if its primary key is referenced by another table's foreign key. So it hurls ORA-02449.
So, given this set up:
create table t_parent (
id number not null
, constraint tp_pk primary key (id)
);
create table t_child (
id number not null
, p_id number not null
, constraint tc_pk primary key (id)
, constraint tc_tp_fk foreign key (p_id)
references t_parent (id)
);
There are three ways to drop table t_parent.
run drop table t_child first: no child table, no foreign key.
Remove the blocking foreign key: alter table t_child drop constraint tc_pc_fk.
A variant on the previous one, let the database figure out the foreign keys:
drop table t_parent cascade constraints.
The first option is the most proper, because it leaves the database in a valid state (no tables, no possibility of data integrity corruption). The valid use for the third approach is a script which razes all the tables from a schema: it's easy to generate such a script from the data dictionary.
The order that you drop or create tables are important because if you have foreign keys referencing another table, you cant delete that table before deleting your own table.
In this example, the Borrower table has to be dropped before the BorrowerType table.

Resources