Django migration id char column to existing table - django

I am trying to create an ID charfield column and set it as primary key to an existing table. So because it is an existing table when i generate Migrations django asks me to provide a default value. What can i enter as primary key default for existing rows? If i enter a single string e.x 'abcd' i will get error for existing rows on migrate because of duplicate value

Apologies.
I didn't read correctly.

Related

Oracle APEX 21.2.0 display image Primary Key

I want to display image in report column. My table has composite primary key: ID and DATE.
When I add these columns in BLOB attributes as Primary Key Column 1 and Primary Key Column 2 report can not find data because of DATE column. Is it a problem in date format, or something else?
I'd suggest you to use only one column as a primary key column (a sequence or - if your database version supports it - an identity column).
Combination of [ID, DATE] you currently have can then be set to unique key (set both columns NOT NULL to "mimic" what primary key would do).
Why? Although your data model probably is just fine, certain Apex functionalities "suffer" from such things and prefer having a single-column primary keys.

How to create a field with a list of choices but store the index?

I'm making a Microsoft Access table where one of the fields is a list of pre-made options. When I make a SQL query on that table it returns the values of the list as strings containing the spelled out choice. I would like to assign numerical values to each element of the list so a SQL query returns a number instead. How do I do this? I know it's possible because I have an access file with such a list but I'm unable to recreate it.
An easy way to do this is to have your combo box use a query of the table as a Rowsource. This query would have the table unique ID in the first field and the field you wish to return as the second field. Then change the setting on the combo box for "Column Count" to 2. If you want to show both fields change the "Column Widths" value to 1"; 1". If you want to show only one field, change the value of one you do not want to see to 0. Now we you refer to this list in an SQL queries, it will use the ID field but show the user the string field.

Django Migrate - row has an invalid foreign key but row does not exist

When migrating my database, I get the following error:
The row in table 'project_obicase' with primary key '2325' has an invalid foreign key: project_obicase.ckId_id contains a value '2443' that does not have a corresponding value in project_pupiladdressck.id.
Looking in my /admin/ site i cannot find this record '2325'. It skips from 2324 to 2333
project_obicase table:
Is there any way to resolve this foreign key mishap if I cannot locate the object? I'd be happy to remove record 2325 if I can find it.
Thanks
I solved this problem by deleting the records manually from the DB shell. (as the records did not appear on the front end)
manage.py dbshell
delete from table
WHERE NOT EXISTS (SELECT 1 FROM other_table t WHERE table.id = other_table.foreign_key)

db2 cannot drop foreign key with lowercase name

I am trying to drop a foreign key in DB2 through the command line. I have succeeded in this many times and I am sure that I am using the correct syntax:
db2 "alter table TABLENAME drop constraint fk_keyname"
Output:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0204N "FK_KEYNAME" is an undefined name. SQLSTATE=42704
All my foreign keys are created with an uppercase name. Except for the key I now want to drop. I don't know how to got created with a lowercase name but it seems that it will not drop keys that are lowercase.
When I try to add this foreign key (while it still exists) I get the following message:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0601N The name of the object to be created is identical to the existing
name "fk_keyname" of type "FOREIGN KEY". SQLSTATE=42710
Does anyone know how to drop foreign keys that have a lowercase name?
The answer by mustaccio worked. Tried all kinds of quotes but this way did the trick:
db2 'alter table TABLENAME drop constraint "fk_keyname"'
DB2 will convert object names to uppercase, unless they are quoted. Generally it's not a very good idea to create objects with lower- or mixed-case names. If your foreign key is actually "fk_keyname" (all lowercase), run db2 "alter table TABLENAME drop constraint \"fk_keyname\"" or db2 'alter table TABLENAME drop constraint "fk_keyname"'
This behaviour is not unique to DB2, by the way.

Trying to add foreign key in mysql with heidisql

I've been trying to add a foreign key to my table using heidisql and I keep getting the error 1452.
After reading around I made sure all my tables were running on InnoDB as well as checking that they had the same datatype and the only way I can add my key is if I drop all my data which I don't intend to do since I have spent quite a few hours on this.
here is my table create code:
CREATE TABLE `data` (
`ID` INT(10) NOT NULL AUTO_INCREMENT,
#bunch of random other columns stripped out
`Ability_1` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0',
#more stripped tables
`Extra_Info` SET('1','2','3','Final','Legendary') NOT NULL DEFAULT '1' COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`ID`),
UNIQUE INDEX `ID` (`ID`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=650;
here is table 2
CREATE TABLE `ability` (
`ability_ID` SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
#stripped columns
`Name_English` VARCHAR(12) NOT NULL COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`ability_ID`),
UNIQUE INDEX `ability_ID` (`ability_ID`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=165;
Finally here is the create code along with the error.
ALTER TABLE `data`
ADD CONSTRAINT `Ability_1` FOREIGN KEY (`Ability_1`) REFERENCES `ability` (`ability_ID`) ON UPDATE CASCADE ON DELETE CASCADE;
/* SQL Error (1452): Cannot add or update a child row: a foreign key constraint fails (`check`.`#sql-ec0_2`, CONSTRAINT `Ability_1` FOREIGN KEY (`Ability_1`) REFERENCES `ability` (`ability_ID`) ON DELETE CASCADE ON UPDATE CASCADE) */
If there is anything else I can provide please let me know this is really bothering me. I'm also using 5.5.27 - MySQL Community Server (GPL) that came with xampp installer.
If you are using HeidiSQL it is pretty easy.
Just see the image, click on the +Add to add foreign keys.
I prefer GUI way of creating tables and its attribute because it saves time and reduces errors.
I found it. Sorry everyone. The problem was that I had 0 as a default value for my fields while my original table had no value for 0.
Here is how you can do it ;
Create your Primary keys. For me this was straight forward so I won't post how to do that here
To create your FOREIGN KEYS you need to change the table / engine type for each table from MyIASM to InnoDb. To do this Select the table on the right hand side then select the OPTIONS tab on the right hand side and change the engine from MyIASM to InnoDb for every table.