Tuesday, September 21, 2010

MySQL->PostgreSQL

Since I had lots of problems with cyrillic encodings in MySQL, I've decided to move to PostgreSQL.
I've changed my SQL scheme a bit: auto increment PK was converted to serial in PostgreSQL.
Also, I've decided to use Django, so first of all I described my DB model:

from django.db import models

class Cues(models.Model):
    data = models.CharField(max_length=50)
   
    def __unicode__(self):
            return self.data

class Reacts(models.Model):
    data = models.CharField(max_length=100)  

    def __unicode__(self):
            return self.data

class Weights(models.Model):
    cue=models.ForeignKey(Cues)   
    react = models.ForeignKey(Reacts)
    weight=models.FloatField()    

After that I executed scripts:
python manage.py validate
python manage.py sqlall avs
python manage.py syncdb

and it automatically created schemas:
CREATE TABLE avs_cues
(
  id serial NOT NULL,
  data character varying(50) NOT NULL,
  CONSTRAINT avs_cues_pkey PRIMARY KEY (id)
)


CREATE TABLE avs_reacts
(
  id serial NOT NULL,
  data character varying(100) NOT NULL,
  CONSTRAINT avs_reacts_pkey PRIMARY KEY (id)
)



CREATE TABLE avs_weights
(
  id serial NOT NULL,
  cue_id integer NOT NULL,
  react_id integer NOT NULL,
  weight double precision NOT NULL,
  CONSTRAINT avs_weights_pkey PRIMARY KEY (id),
  CONSTRAINT avs_weights_cue_id_fkey FOREIGN KEY (cue_id)
      REFERENCES avs_cues (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
  CONSTRAINT avs_weights_react_id_fkey FOREIGN KEY (react_id)
      REFERENCES avs_reacts (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
WITH (
  OIDS=FALSE
);
ALTER TABLE avs_weights OWNER TO postgres;

-- Index: avs_weights_cue_id

-- DROP INDEX avs_weights_cue_id;

CREATE INDEX avs_weights_cue_id
  ON avs_weights
  USING btree
  (cue_id);

-- Index: avs_weights_react_id

-- DROP INDEX avs_weights_react_id;

CREATE INDEX avs_weights_react_id
  ON avs_weights
  USING btree
  (react_id);
! The problem is that framework doesn't allow to create composite foreign key



Example of query (returns cue and number od reaction for this cue):
select avs_cues.data, count(react_id) from avs_weights,avs_cues where avs_cues.id=avs_weights.cue_id group by avs_cues.data;

No comments:

Post a Comment