I used the unique_together between to fields and after some time and new requirements I've decided I want to remove it so I deleted it from the models definition, but still it seems to be causing "Duplicate entry" problems.
Do I have to drop some constraint on the database? in that case, how can I do it (I use phpmyadmin).
Thank You
There will be a compound UNIQUE index on the table. Drop that.
Related
I'm having a bit of an issue with circular foreign keys. I have a table posts with a uid column that is a BIGINT NOT NULL PRIMARY KEY, and I need to reference that in the last_seen_post column of my users table. The problem is that there's a sender_id column in posts that references users(user_id), meaning that I can't use both foreign keys at the same time because depending on the order of table definition, one of them will not be defined. Any fixes other than to drop the foreign key on the last_seen_post column?
I really would rather avoid dropping either foreign keys because the data integrity for this backend is mission critical. I know I could probably make another table to achieve this, but I was really hoping to get away with this since it's way cheaper in terms of space and search time
For context, the last_seen_post column is supposed to help the backend figure out what the user's "feed" is, i.e. the collection of posts from users they follow that they have not downloaded yet.
Alright, I figured it out, so I'm gonna leave the solution here if others need it in the future. The easiest thing to do is to create the column without the foreign key constraint and then do ALTER TABLE yourtbl ADD FOREIGN KEY (yourcolumn) REFERENCES othertbl(othercolumn); once both tables have been created!
I'm attempiting to use cx_OracleTool's CopyData.py script to copy data between two tables on separate Oracle schemas/instances:
http://cx-oracletools.sourceforge.net/cx_OracleTools.html
When I run it against my tables, I get the error:
No primary or unique constraint found on table.
I don't know much about Oracle, to be honest, but from what I can tell the tables don't seem to have any PK constraint or anything like that defined.
The merits of this aside, I think it's simply been setup that way for expediency, and it's unlikely to change anytime nearterm.
Is there any way to get copyData.py to run in this scenario without a PK constraint?
Cheers,
Victor
The issue is that CopyData checks to see if the row exists in the destination table, and it can't do that without a unique key.
If it is acceptable to insert all rows and not update changed ones, use the --no-check-exists option. According to the code this will bypass the primary key check.
Otherwise, use the --key-columns=COLS option to manually specify the columns to be used as the unique key. This will also bypass the primary key check.
I have created tables in sql server. And i have also inserted data/rows in that tables.
Now i want to make relationship among them means i want to create foreign key constraints among them, is it possible ?
Whenever i try to create relationship among table a problem is occured. "Saving changes is permitted, The changes you made required table to re-created and dropped"
Please suggest me what should i do to make relationship(foreign key) among them ?
My Child table design is this
this is my parent table:-
please now right what alter query i should write..?
You can try this link
"Error message when you try to save a table in SQL Server 2008: "Saving changes is not permitted"
Another solution is below.
I think the problem is because of a feature when using the GUI. If you have a look at this link it shows you how to work round it. It is a feature which prevents you from dropping and recreating the table which is what SSMS does in the background when you click ok.
The code provided by the previous posted is the best way to do this.
You could do this with a script like this:
alter table ChildTable
add constraint FK_ChildTable_ColumnName foreign key (ColumnName) references ParentTable(PrimaryKeyColumnName)
[Edit] If I read your description correctly the script would be:
alter table emp
add constraint FK_emp_salary foreign key(salary) references testing(roll)
You can only add foreign constraints that aren't violated by existing data. You may also have to add suitable indices first, although this depends on the DBMS. In any case, first make sure your existing data is compatible with the foreign keys you want to introduce. In other words, if you were to create the foreign key first, and then insert the data, you should not produce any foreign key violations.
I created a constraint on my tsql table like this:
alter table disabledqualities
add constraint uc_uIdQualCode
unique (userId, qualitycode)
In MSSMStudio the constraint shows up under indexes rather then under constraints.
Why?
--EDIT--
I understand that it creates an index to enforce the constraint, but then why is there a node called "constraints"?
SQL Server creates an index behind the scene to enforce the constraint
here is another way of writing that by adding nonclustered telling sql server to use a nonclustered index, you can also create a clustered on providing that you don't have a PK that is clustered (the default) or another clustered index already
alter table disabledqualities
add constraint uc_uIdQualCode
unique nonclustered (userId, qualitycode)
[edit]
that node is to add check constraint, unique constraints are added under indexes
either way stay away from wizards
Check constraints and default constraints are shown under the constraints node.
SQL uses indexes to enforce unique constraints.
I understand that it creates an index to enforce the constraint, but then why is there a node called "constraints"?
This node is to display CHECK constraints.
The true answer is "because Microsoft said so. If you want the answer, you'll have to ask them".
Because UNIQUE constraints can be found in sysindexes system view
select * from sysindexes where name = 'uc_uIdQualCode'
It is simply logical
To add new Unique constaint from Studio you need to click Manage Index and Keys button. So if you are adding it from Indexes, you have to see it in Indexes :)
I'm thinking perhaps you don't understand what might show up in constraints becasue you don't know what a check constraint is. A check constraint will check the data on insert or update to see if it meets some sort of business rule. It is used for ensuring data integrity. For instance if you have an integer field that should only contain the values of 1,4 or 5 then you would set up a check constraint to make sure that 9 isn't ever added to the field. A check constraint on date field might specify that it must be later than the current date and time for a field that is the PlannedCompletionDate or that CompletionDate must be later than StartDate. These are the kind of things that show up under constraints.
What is the best way to delete a table referenced by a foreign keys?
Is the intended goal to orphan those records and never use the foreign key again? If so the method mentioned before about disabling the key is fine, otherwise you may want to instead delete the records referencing the table you want to delete first (or update the to point to a more appropriate record, or NULL if that makes sense in this case). I seem to be coming at this from a different direction than others, are you sure the foreign key is pointless, and if so why not just remove it? At some point someone wanted to constraint this behavior, before just disabling constraints I make sure I understand their purpose and have a good justification for bypassing those safeguards.
Remove the foreign key constraint and then delete the table once no-one is forced to recognize it. If the column in the second table (the one not being deleted) is not used elsewhere, then you should probably delete the whole column after removing the constraint.
You need to remove the constraint before you're allowed to delete the table referenced by it. SQL Server uses the following syntax:
ALTER TABLE <table_name> DROP FOREIGN KEY <foreignkey_name>
Keep in mind that the constraint exists on the table that references the one you want to delete so that's the table you should be altering.
Do NOT delete a table with foreign key constraints without considering the impact on the foreign key tables. Let me explain the impact of simply deleting the foreign key and then the table with an example.
Consider two tables - parts and orderdetails. There is a foreign key constraint that says a part must exist before it can be put into the orderdetails table. What is stored in the orderdetail table is the id for the part from the parts table, not the part name or description. Suppose you drop the foreign key and then drop the parts table. Now all the data in the orderdetail table is totally useless because you have no way of knowing what the part ordered was. This would include orders not yet shipped and orders that the customer might call and ask questions about. Further you now have no way to recreate that data except by restoring a backup (hope you have one).
Further suppose you want to drop the table and recreate it to make a change to the table. Then reload the information and put the foreign key back on. In this case you should probaly use alter table instead of drop and recreate but if you don't you may end up with id numbers that are not the same as they were originally and thus now the orders will reference the wrong ids. This can be done safely but you would have to do it very carefully and with a lot of thought as to the consequences.
by using On Delete Cascade