 COMMENT ON COLUMN calendar_element.positive IS 'agencement des calendar_element d''un meme calendrier : ajout, soustraction, intersection avec les precedents';
 COMMENT ON COLUMN calendar_element."interval" IS 'intervalle de repetition en cas de calendrier recurrent (lundi)';
 COMMENT ON COLUMN calendar_element.included_calendar_id IS 'id du calendrier inclus';
-CREATE SEQUENCE calendar_element_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-ALTER SEQUENCE calendar_element_id_seq OWNED BY calendar_element.id;
-
-
-
+ALTER TABLE ONLY calendar_element ADD CONSTRAINT calendar_element_pk PRIMARY KEY (id);
+CREATE INDEX calendar_element_calendar_id_idx ON calendar_element USING btree (calendar_id);
+ALTER TABLE ONLY calendar_element ADD CONSTRAINT calendar_element_calendar_id_fk FOREIGN KEY (calendar_id) REFERENCES calendar(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
+ALTER TABLE ONLY calendar_element ADD CONSTRAINT calendar_element_included_calendar_id_fk FOREIGN KEY (included_calendar_id) REFERENCES calendar(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
 
 CREATE TABLE calendar_link (
-    id integer NOT NULL,
+    id serial,
     trip_id integer NOT NULL,
     day_calendar_id integer NOT NULL,
     period_calendar_id integer NOT NULL
 );
 COMMENT ON TABLE calendar_link IS 'Lien entre les calendriers et les services (trip) de production auxquels il s''applique.';
-CREATE SEQUENCE calendar_link_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-ALTER SEQUENCE calendar_link_id_seq OWNED BY calendar_link.id;
-
-
-
+ALTER TABLE ONLY calendar_link ADD CONSTRAINT calendar_link_pk PRIMARY KEY (id);
+CREATE INDEX calendar_link_day_calendar_id_idx ON calendar_link USING btree (day_calendar_id);
+CREATE INDEX calendar_link_period_calendar_id_idx ON calendar_link USING btree (period_calendar_id);
+CREATE INDEX calendar_link_trip_id_idx ON calendar_link USING btree (trip_id);
+ALTER TABLE ONLY calendar_link ADD CONSTRAINT calendar_link_day_calendar_id_fk FOREIGN KEY (day_calendar_id) REFERENCES calendar(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
+ALTER TABLE ONLY calendar_link ADD CONSTRAINT calendar_link_period_calendar_id_fk FOREIGN KEY (period_calendar_id) REFERENCES calendar(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
+ALTER TABLE ONLY calendar_link ADD CONSTRAINT calendar_link_trip_id_fk FOREIGN KEY (trip_id) REFERENCES trip(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
 
 CREATE TABLE change_cause (
-    id integer NOT NULL,
+    id serial,
     description character varying(255)
 );
 COMMENT ON TABLE change_cause IS 'Motif de creation d''une nouvelle line_version.';
-CREATE SEQUENCE change_cause_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-ALTER SEQUENCE change_cause_id_seq OWNED BY change_cause.id;
-
-
-
+ALTER TABLE ONLY change_cause ADD CONSTRAINT change_cause_pk PRIMARY KEY (id);
 
 CREATE TABLE change_cause_link (
-    id integer NOT NULL,
+    id serial,
     change_cause_id integer,
     line_version_id integer
 );
 COMMENT ON TABLE change_cause_link IS 'Lien entre les motifs de nouvelle line_version et la line_version.';
-CREATE SEQUENCE change_cause_link_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-ALTER SEQUENCE change_cause_link_id_seq OWNED BY change_cause_link.id;
-
-
-
+ALTER TABLE ONLY change_cause_link ADD CONSTRAINT change_cause_link_pk PRIMARY KEY (id);
+ALTER TABLE ONLY change_cause_link ADD CONSTRAINT change_cause_link_change_cause_id_fk FOREIGN KEY (change_cause_id) REFERENCES change_cause(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
+ALTER TABLE ONLY change_cause_link ADD CONSTRAINT change_cause_link_line_version_id_fk FOREIGN KEY (line_version_id) REFERENCES line_version(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
 
 CREATE TABLE city (
-    id integer NOT NULL,
+    id serial,
     insee character varying(5) NOT NULL,
     name character varying(255) NOT NULL,
     main_stop_area_id integer,
     the_geom geometry(Polygon,3943)
  );
 COMMENT ON TABLE city IS 'Commune.';
-CREATE SEQUENCE city_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-ALTER SEQUENCE city_id_seq OWNED BY city.id;
-
-
-
+ALTER TABLE ONLY city ADD CONSTRAINT city_pk PRIMARY KEY (id);
+ALTER TABLE ONLY city ADD CONSTRAINT city_main_stop_area_id_fk FOREIGN KEY (main_stop_area_id) REFERENCES stop_area(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
 
 CREATE TABLE comment (
-    id integer NOT NULL,
+    id serial,
     label character varying(5),
     comment_text character varying(255)
 );
 COMMENT ON TABLE comment IS 'Note sur un itineraire (route) ou un service (trip). Signale une particularite sur les fiches horaire.';
 COMMENT ON COLUMN comment.label IS 'Lettre servant a signer le commentaire.';
 COMMENT ON COLUMN comment.comment_text IS 'Description textuelle du commentaire.';
-CREATE SEQUENCE comment_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-ALTER SEQUENCE comment_id_seq OWNED BY comment.id;
-
-
-
+ALTER TABLE ONLY comment ADD CONSTRAINT comment_pk PRIMARY KEY (id);
 
 CREATE TABLE datasource (
-    id integer NOT NULL,
+    id serial,
     name character varying(30) NOT NULL,
     agency_id integer NOT NULL
 );
 COMMENT ON TABLE datasource IS 'Referentiel fournisseur de donnees. Les lignes saisies manuellement ont pour referentiel le ''Service donnes''.';
-CREATE SEQUENCE datasource_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-ALTER SEQUENCE datasource_id_seq OWNED BY datasource.id;
-
-
-
+ALTER TABLE ONLY datasource ADD CONSTRAINT datasource_pk PRIMARY KEY (id);
+ALTER TABLE ONLY datasource ADD CONSTRAINT datasource_id_agency_fk FOREIGN KEY (agency_id) REFERENCES agency(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
 
 CREATE TABLE exception_type (
-    id integer NOT NULL,
+    id serial,
     label character varying(5),
     exception_text character varying(255),
     grid_calendar_pattern character varying(7),

    phone character varying(20)
);
COMMENT ON TABLE agency IS 'Reseau de transport en commun. Contient egalement le fuseau horaire et la langue.';
CREATE SEQUENCE agency_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
ALTER SEQUENCE agency_id_seq OWNED BY agency.id;




CREATE TABLE alias (
    id integer NOT NULL,
    stop_area_id integer,
    name character varying(255)
);
COMMENT ON TABLE alias IS 'Alias des zones d''arrets.';
CREATE SEQUENCE alias_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
ALTER SEQUENCE alias_id_seq OWNED BY alias.id;




CREATE TABLE calendar (
    id integer NOT NULL,
    name character varying(50) NOT NULL,
    calendar_type integer NOT NULL
);
COMMENT ON TABLE calendar IS 'Le calendrier d''application des services en production. Il est lui-meme compose de calendar_element.';



CREATE TABLE calendar_datasource (
    id integer NOT NULL,
    calendar_id integer NOT NULL,
    datasource_id integer NOT NULL,
    code character varying(20)
);
COMMENT ON TABLE calendar_datasource IS 'Reference de l''objet dans le referentiel de la datasource.';
CREATE SEQUENCE calendar_datasource_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
ALTER SEQUENCE calendar_datasource_id_seq OWNED BY calendar_datasource.id;
CREATE SEQUENCE calendar_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
ALTER SEQUENCE calendar_id_seq OWNED BY calendar.id;




CREATE TABLE calendar_element (
    id integer NOT NULL,
    calendar_id integer NOT NULL,
    start_date date,
    end_date date,

COMMENT ON TABLE stop_time IS 'Horaire d''un troncon d''itineraire.';
COMMENT ON COLUMN stop_time.arrival_time IS 'Temps en seconde apres minuit de la date. Peut depasser 23h59.';
COMMENT ON COLUMN stop_time.departure_time IS 'Temps en seconde apres minuit de la date. Peut depasser 23h59.';
ALTER TABLE ONLY stop_time ADD CONSTRAINT stop_time_pk PRIMARY KEY (id);
ALTER TABLE ONLY stop_time ADD CONSTRAINT stop_time_route_stop_id_fk FOREIGN KEY (route_stop_id) REFERENCES route_stop(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
ALTER TABLE ONLY stop_time ADD CONSTRAINT stop_time_trip_id_fk FOREIGN KEY (trip_id) REFERENCES trip(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
CREATE INDEX stop_time_trip_id_idx ON stop_time USING btree (trip_id);
CREATE INDEX stop_time_route_stop_id_idx ON stop_time USING btree (route_stop_id);

CREATE TABLE transfer (
    id serial,
    start_stop_id integer NOT NULL,
    end_stop_id integer NOT NULL,
    duration integer NOT NULL,
