EF4 generates invalid script - visual-studio-2010

When I right-click in a .EDMX file and click Generate Database From Model, the resulting script is obviously wrong because of the table names. What it generates is the following script. Note the table names in the DROP TABLE part versus the CREATE TABLE part.
Why is this inconsistent?
This is obviously not a reusable script. What I created was an Entity named "Address" and an Entity named "Company", etc (all singular). The EntitySet names are pluralized. The "Pluralize New Objects" boolean does not change this either. So what's the deal?
For what it's worth, I originally generated the EDMX by pointing it to a database that had tables with non-pluralized names and now that I've made some changes, I want to go back the other way. I'd like to have the option to go back and forth as neither the db-first nor the model-first model is ideal in all scenarios, and I have the control to ensure that there will be no merging issues from multiple people going both ways at the same time.
-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- NOTE: if the constraint does not exist, an ignorable error will be reported.
-- --------------------------------------------------
ALTER TABLE [Address] DROP CONSTRAINT [FK_Address_StateID-State_ID];
GO
ALTER TABLE [Company] DROP CONSTRAINT [FK_Company_AddressID-Address_ID];
GO
ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_BossEmployeeID-Employee_ID];
GO
ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_CompanyID-Company_ID];
GO
ALTER TABLE [Employee] DROP CONSTRAINT [FK_Employee_PersonID-Person_ID];
GO
ALTER TABLE [Person] DROP CONSTRAINT [FK_Person_AddressID-Address_ID];
GO
-- --------------------------------------------------
-- Dropping existing tables
-- NOTE: if the table does not exist, an ignorable error will be reported.
-- --------------------------------------------------
DROP TABLE [Address];
GO
DROP TABLE [Company];
GO
DROP TABLE [Employee];
GO
DROP TABLE [Person];
GO
DROP TABLE [State];
GO
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table 'Addresses'
CREATE TABLE [Addresses] (
[ID] int IDENTITY(1,1) NOT NULL,
[StreetAddress] nvarchar(100) NOT NULL,
[City] nvarchar(100) NOT NULL,
[StateID] int NOT NULL,
[Zip] nvarchar(10) NOT NULL
);
GO
-- Creating table 'Companies'
CREATE TABLE [Companies] (
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(100) NOT NULL,
[AddressID] int NOT NULL
);
GO
-- Creating table 'People'
CREATE TABLE [People] (
[ID] int IDENTITY(1,1) NOT NULL,
[FirstName] nvarchar(100) NOT NULL,
[LastName] nvarchar(100) NOT NULL,
[AddressID] int NOT NULL
);
GO
-- Creating table 'States'
CREATE TABLE [States] (
[ID] int IDENTITY(1,1) NOT NULL,
[Name] nvarchar(100) NOT NULL,
[Abbreviation] nvarchar(2) NOT NULL
);
GO
-- Creating table 'Employees'
CREATE TABLE [Employees] (
[ID] int IDENTITY(1,1) NOT NULL,
[PersonID] int NOT NULL,
[CompanyID] int NOT NULL,
[Position] nvarchar(100) NOT NULL,
[BossEmployeeID] int NULL
);
GO
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [ID] in table 'Addresses'
ALTER TABLE [Addresses]
ADD CONSTRAINT [PK_Addresses]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [PK_Companies]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'People'
ALTER TABLE [People]
ADD CONSTRAINT [PK_People]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'States'
ALTER TABLE [States]
ADD CONSTRAINT [PK_States]
PRIMARY KEY ([ID] );
GO
-- Creating primary key on [ID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [PK_Employees]
PRIMARY KEY ([ID] );
GO
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [StateID] in table 'Addresses'
ALTER TABLE [Addresses]
ADD CONSTRAINT [FK_Address_StateID_State_ID]
FOREIGN KEY ([StateID])
REFERENCES [States]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Address_StateID_State_ID'
CREATE INDEX [IX_FK_Address_StateID_State_ID]
ON [Addresses]
([StateID]);
GO
-- Creating foreign key on [AddressID] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [FK_Company_AddressID_Address_ID]
FOREIGN KEY ([AddressID])
REFERENCES [Addresses]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Company_AddressID_Address_ID'
CREATE INDEX [IX_FK_Company_AddressID_Address_ID]
ON [Companies]
([AddressID]);
GO
-- Creating foreign key on [AddressID] in table 'People'
ALTER TABLE [People]
ADD CONSTRAINT [FK_Person_AddressID_Address_ID]
FOREIGN KEY ([AddressID])
REFERENCES [Addresses]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Person_AddressID_Address_ID'
CREATE INDEX [IX_FK_Person_AddressID_Address_ID]
ON [People]
([AddressID]);
GO
-- Creating foreign key on [CompanyID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_CompanyID_Company_ID]
FOREIGN KEY ([CompanyID])
REFERENCES [Companies]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_CompanyID_Company_ID'
CREATE INDEX [IX_FK_Employee_CompanyID_Company_ID]
ON [Employees]
([CompanyID]);
GO
-- Creating foreign key on [BossEmployeeID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_BossEmployeeID_Employee_ID]
FOREIGN KEY ([BossEmployeeID])
REFERENCES [Employees]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_BossEmployeeID_Employee_ID'
CREATE INDEX [IX_FK_Employee_BossEmployeeID_Employee_ID]
ON [Employees]
([BossEmployeeID]);
GO
-- Creating foreign key on [PersonID] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_Employee_PersonID_Person_ID]
FOREIGN KEY ([PersonID])
REFERENCES [People]
([ID])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_Employee_PersonID_Person_ID'
CREATE INDEX [IX_FK_Employee_PersonID_Person_ID]
ON [Employees]
([PersonID]);
GO
-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------

I have been unable to find an answer to this problem. In addition, I have had issues recreating this problem where I'm not going back and forth from DB-first to model-first scenarios. As such, I'm just assuming that back-and-forth has caused issues and is a bad situation to put EF through.

Related

drop foreign key constraint on DB2 table through alter table

I have a DB2 (for IBMi) table created as below. I would like to drop the forign key constraint while running in an SQLRPGLE program. Is this possible?
create table grid_action_details(id integer not null
generated always as identity
(start with 1 increment by 1)
PRIMARY KEY,grid_details_id integer,foreign key(grid_details_id)
references grid_details(id),action_code_details_id integer,
foreign key(action_code_details_id)
references action_code_details(id),action_code_status varchar(2),
created_date date default
current_date,created_by varchar(30),
last_updated_date date default current_date,updated_by
varchar(30),required_parameter clob);
I tried the below syntax but it just doesn't seem to work for me:
ALTER TABLE table-name
DROP FOREIGN KEY foreign_key_name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_id
ACTION_CODE_DETAILS_ID in IESQAFILE type *N not found.
You drop the FK constraint via name, not via column.
Since you didn't specify one during create, you'll need to look to see what name the system generated.
Always a best practice to name things yourself.
CONSTAINT <name> FOREIGN KEY (<columns>)
create table grid_action_details (
id integer not null generated always as identity (
start with 1 increment by 1
)
,constraint grid_action_details_pk
primary key
,grid_details_id integer
,constraint grid_details_fk
foreign key (grid_details_id)
references grid_details (id)
,action_code_details_id integer
,constraint action_code_details_fk
foreign key (action_code_details_id)
references action_code_details (id)
,action_code_status varchar(2)
,created_date date default current_date
,created_by varchar(30)
,last_updated_date date default current_date
,updated_by varchar(30)
,required_parameter clob
);
Now you can drop by the known name
alter table iesqafile.grid_action_details
drop foreign key action_code_details_fk
EDIT
To find the generated name use:
the ACS Schema component
DSPFD
SQL against one of the catalog views (QSYS2.SYSCST, SYSIBM.SQLFOREIGNKEYS, SYSIBM.REFERENTIAL_CONSTRAINTS )

I'm trying to create foreign key between two tables but got: ORA-02270

Maybe I'm burnout, but I don't understand this. I have two tables in Oracle: TBL_a and TBL_x. I'm trying to create a foreign key between thos two tables as follows and get error
ORA-02270: no matching unique or primary key.
CREATE TABLE tbl_a (
cod_op integer,
cod_dni char(8),
cod_correl integer,
varchar2(50)
);
CREATE TABLE tbl_x (
cod_op integer,
cod_dni char(8),
blabla varchar2(50)
);
CREATE UNIQUE INDEX TBL_A_PK ON TBL_A (COD_OP);
CREATE UNIQUE INDEX TBL_x_PK ON TBL_x (COD_OP);
ALTER TABLE TBL_a ADD CONSTRAINT TBL_a_R01
FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP);
The problem is that you've created unique INDEXES on your tables, but you didn't create a unique or primary key CONSTRAINT. Oracle requires that the constraints exist in order to establish a foreign key relationship.
If you drop your existing indexes and add the appropriate constraints you can establish your foreign key relationship:
DROP INDEX TBL_A_PK;
DROP INDEX TBL_x_PK;
ALTER TABLE TBL_A
ADD CONSTRAINT UQ_A
UNIQUE(COD_OP)
USING INDEX;
ALTER TABLE TBL_X
ADD CONSTRAINT UQ_X
UNIQUE(COD_OP)
USING INDEX;
dbfiddle here
The table that is referred by the foreign key (here, tbl_x) must have a primary key or a unique constraint.
In your use case, as you are declaring a unique index on cod_op, you could simply make cod_op the primary key of tbl_x instead: that would make the error disappear.
Demo on DB Fiddle
In general, it is a good practice to have a primary key on any table. Extending the principe of turning your unique indexes to primary keys, your DDL statements could be simplified as follows:
CREATE TABLE tbl_x (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
blabla VARCHAR2(50)
);
CREATE TABLE tbl_a (
cod_op INTEGER PRIMARY KEY,
cod_dni CHAR(8),
cod_correl INTEGER,
blabla VARCHAR2(50),
CONSTRAINT TBL_a_R01 FOREIGN KEY (COD_OP) REFERENCES TBL_x (COD_OP)
);
Demo on DB Fiddle

While creating table in oracle database it is showing right parenthesis missing error

While creating table in oracle database it is showing right parenthesis missing error
CREATE TABLE Catalog(
sid VARCHAR2(50) FOREIGN KEY REFERENCES Suppliers(sid),
pid VARCHAR2(50) FOREIGN KEY REFERENCES Parts(pid),
quantity VARCHAR2(50)
);
You need to remove the parts FOREIGN KEY as :
CREATE TABLE Catalog(
sid VARCHAR2(50) REFERENCES Suppliers(sid),
pid VARCHAR2(50) REFERENCES Parts(pid),
quantity VARCHAR2(50)
);
or you may create as :
CREATE TABLE Catalog
( sid VARCHAR2(50),
pid VARCHAR2(50),
quantity VARCHAR2(50),
CONSTRAINT fk_supplier
FOREIGN KEY (sid)
REFERENCES suppliers(sid),
CONSTRAINT fk_parts
FOREIGN KEY (pid)
REFERENCES parts(pid)
);
by defining FOREIGN CONSTRAINTS' names
or if the table already has been created before, by the following way, the constraints may be added later :
CREATE TABLE Catalog
( sid VARCHAR2(50),
pid VARCHAR2(50),
quantity VARCHAR2(50)
);
ALTER TABLE Catalog
ADD CONSTRAINT fk_supplier
FOREIGN KEY (sid)
REFERENCES suppliers(sid);
ALTER TABLE Catalog
ADD CONSTRAINT fk_parts
FOREIGN KEY (pid)
REFERENCES parts(pid);
Constraint syntax is documented here.
inline_constraint::=
out_of_line_constraint::=
references_clause::=
Your examples are inline, as they are part of the column definition rather than in a separate section or statement.
I would write it as:
create table catalog
( sid references suppliers(sid)
, pid references parts(pid)
, quantity varchar2(50) );
or if I wanted to name them explicitly:
create table catalog
( sid constraint cat_supplier_fk references suppliers(sid)
, pid constraint cat_part_fk references parts(pid)
, quantity varchar2(50) );
You can explicitly override the default datatype if you want, but I consider it better practice to allow it to inherit from the parent. (You can only do this with inline constraints.)

Oracle foreign key allow one extra specific value

I have one simple table:
-- Create table
create table FAVOURITE_RULES
( rule_id NUMBER(9) not null,
user_id NUMBER(9) not null);
-- Create/Recreate primary, unique and foreign key constraints
alter table FAVOURITE_RULES
add constraint FAV_RULES_PK primary key (RULE_ID, USER_ID)
alter table FAVOURITE_RULES
add constraint FAV_RULES_RULE_ID_FK foreign key (RULE_ID)
references RULES (RULE_ID) on delete cascade;
alter table FAVOURITE_RULES
add constraint FAV_RULES_USER_ID foreign key (USER_ID)
references USER_AUTHENTICATION (USER_ID) on delete cascade;
I have a rule (from .Net code) that doesn't exist in the original table RULES. It has the Id=-999.
When I try to insert into the FAVOURITE_RULES I get an error about integrity constraint violation (as expected) (FAV_RULES_RULE_ID_FK) violated - parent key not found.
Can I keep the foreign key (FAV_RULES_RULE_ID_FK ) and allow extra only this value (-999) to be inserted?
May be this can help.
Step 1: drop fk constraint
Step 2: insert your violating row
Step 3: again create fk constraint with ENABLE NOVALIDATE

Do I need to drop a foreign key on one table to delete a row on another using oracle?

I have two tables
Parent table
(account_number varchar(15) not null,
branch_name varchar(50) not null,
balance number not null,
primary key(account_number));
Child table
account_number varchar(15) not null,
foreign key(account_number) references parent table(account_number));
I am trying this:
DELETE FROM parent table
WHERE balances > 1000;
I am deleting accounts by balances on the parent but I get an error message about the child relationship.
My assumption is a DELETE CASCADE has to be added to the foreign key in the child table. All the documentation shows how to alter the table when the constraint is named. I do not have that situation. Is there a way to do it, or do I have to specify the cascade in the delete statement I am writing?
Every constraint in Oracle has a name. If a name isn't specified when the constraint is created, Oracle will autogenerate a name for the constraint. If you don't know what the name of a constraint is, try running a SQL statement that violates the constraint and reading the constraint name from the error message:
SQL> delete from parent where account_number = 1234;
delete from parent where account_number = 1234
*
ERROR at line 1:
ORA-02292: integrity constraint (LUKE.SYS_C007357) violated - child record
found
In this case the name of the constraint is SYS_C007357.
If that doesn't work, you can query the data dictionary view user_constraints:
SQL> select constraint_name from user_constraints where table_name = 'CHILD' and constraint_type = 'R';
CONSTRAINT_NAME
------------------------------
SYS_C007357
As far as I can tell, you can't modify a foreign key constraint to enable ON DELETE CASCADE. Instead you must drop the constraint and recreate it.
I don't believe you can apply the CASCADE option to a DELETE statement either, but you can delete the child rows before deleting from the parent:
DELETE FROM child
WHERE account_number IN (SELECT account_number FROM parent WHERE balance > 1000);
DELETE FROM parent
WHERE balance > 1000;
However, I don't know how many other tables you have with foreign key constraints referencing your parent table, nor in how many places you are deleting from the parent table, so I can't say how much work it would be to use this approach.
yes you can set DELETE CASCADE
see more info here FOREIGN KEYS WITH CASCADE DELETE
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);
for example
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE
);

Resources