Abhilash Joseph
Abhilash Joseph

Reputation: 1196

Create django model from SQL table definitions

CREATE TABLE `defendant_company_potential` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(100) NOT NULL,
  `address` varchar(100) DEFAULT NULL,
  `city` varchar(50) DEFAULT NULL,
  `state` varchar(4) DEFAULT NULL,
  `zip` varchar(20) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,

  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_defendant_potential_company` (`company_name`,`address`,`city`,`state`),
  KEY `fk_jurisdiction_company` (`jurisdiction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `defendant_defendant_company_potential_map` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `defendant_id` int(11) NOT NULL,
  `defendant_company_potential_id` int(11) NOT NULL,
  `defendant_company_potential_correspondance_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_defendant_defendant_company`
  (`defendant_id`,`defendant_company_potential_id`),
  CONSTRAINT `defendant_id_fk_map` FOREIGN KEY (`defendant_id`) REFERENCES `defendant`(`id`),
  CONSTRAINT `defendant_company_potential_id_fk_map` FOREIGN KEY (`defendant_company_potential_id`) REFERENCES `defendant_company_potential`(`id`)
  CONSTRAINT `defendant_company_potential_correspondance_id_fk` FOREIGN KEY (`defendant_company_potential_correspondance_id`) REFERENCES `defendant_company_potential_correspondance`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `defendant_company_potential_correspondance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `defendant_id` int(11) NOT NULL,
  `defendant_company_potential_id` int(11) NOT NULL,
  `mailed_count` int(11) DEFAULT NULL,
  `phoned_date` datetime DEFAULT NULL,
 PRIMARY KEY (`id`),
  UNIQUE KEY `uk_potential_correspondance`     (`defendant_id`,`defendant_company_potential_id`),
  CONSTRAINT `defendant_id_fk` FOREIGN KEY (`defendant_id`) REFERENCES `defendant`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `defendant_company_potential_id_fk` FOREIGN KEY (`defendant_company_potential_id`)
  REFERENCES `defendant_company_potential`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) 

Defendant table have only one field name and PK is its ID .

My Question is How should i make that map table in Django (I cant change the table structure ) ??? Thank you

Upvotes: 1

Views: 1094

Answers (1)

Aidan Ewen
Aidan Ewen

Reputation: 13308

Have a look at the documentation for integrating existing databases.

It looks like you should be able to create the database table, then have django inspect it and create the model.

python manage.py inspectdb > models.py

Upvotes: 2

Related Questions