Commit a9db1a61 authored by jvfpw18's avatar jvfpw18
Browse files

Add primary key to temporary table after insertion

parent a28b9922
......@@ -25,7 +25,6 @@ from os import chdir
from datetime import datetime
from database.base import MissingTableError
from database.database_table import gen_data_table, copy_tabbed_to_csv
import database.groups
import settings
from database.groups import DATA_GROUP, DATABASE_TABLE_NAME
import pandas as pd
......@@ -53,6 +52,7 @@ def temporary_data(connection, file_name, table, year, offset=2,
table.populate_temporary(ttable, file_name, header, year, delimiters, null, offset, bind=connection)
table.apply_derivatives(ttable, ttable.columns.keys(), year, bind=connection)
table.add_pk_to_temporary(ttable, bind=connection)
return ttable
......
......@@ -28,6 +28,7 @@ import jsbeautifier
from sqlalchemy import Table, Column, inspect, Integer, String, Boolean,\
PrimaryKeyConstraint, ForeignKeyConstraint, text
from sqlalchemy.sql import select, insert, update, delete, func
from sqlalchemy.schema import AddConstraint
import pandas as pd
from database.base import DatabaseColumnError, MissingProtocolError, DatabaseMappingError, \
......@@ -187,18 +188,28 @@ class DatabaseTable(Table):
except InvalidTargetError:
pass
pks = get_primary_keys(self)
primary_key = []
for pk in pks:
primary_key.append(ttable.columns.get(pk.name))
ttable.constraints.add(PrimaryKeyConstraint(*primary_key))
for column in additional:
ttable.append_column(Column(column, String(255)))
return ttable
def add_pk_to_temporary(self, ttable, bind=None):
'''
Add the primary keys constraint to the temporary table
'''
if bind is None:
bind = self.metadata.bind
pks = get_primary_keys(self)
primary_keys = []
for pk in pks:
primary_keys.append(ttable.columns.get(pk.name))
for pk in primary_keys:
new_pk = PrimaryKeyConstraint(pk)
query = AddConstraint(new_pk)
bind.execute(query)
def populate_temporary(self, ttable, in_file, header, year, delimiters=[';', '\\n', '"'],
null='', offset=2, bind=None):
'''
......@@ -235,11 +246,13 @@ class DatabaseTable(Table):
'''
Creates a new temporary table where its data mirrors the original, taken directly from the database
'''
ttable = self.get_temporary(year=year)
ttable.create(bind)
if bind is None:
bind = self.metadata.bind
ttable = self.get_temporary(year=year)
ttable.create(bind)
self.add_pk_to_temporary(ttable, bind)
original_columns = list(self.columns)
query = ttable.insert().from_select(original_columns, select(original_columns)
.where(self.c[settings.YEAR_COLUMN] == year))
......
......@@ -78,7 +78,9 @@ BASE = [
SIMCAQ_AGGREGATE = [
'docente_por_escola.sql',
'idm.sql',
'projecao_matricula.sql'
'projecao_matricula.sql',
'transporte.sql',
'docente_por_formacao.sql'
]
# ---------------------------------------------------------------------------------------#
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment