SQLalchemy select minimum value per period - flask
My problem.
I have flask application using sqlalchemy and sqlite3.
I have data in a table in sqlite3 defined as follows
CREATE TABLE pathloss (
id INTEGER NOT NULL,
timestamp DATETIME,
value FLOAT,
PRIMARY KEY (id)
);
with data example :
221516|2022-03-23 08:30:05.294763|236.82
221517|2022-03-23 08:30:20.940485|236.46
221518|2022-03-23 08:30:31.232806|237.74
221519|2022-03-23 08:30:41.557013|236.62
221520|2022-03-23 08:30:51.858161|234.96
221521|2022-03-23 08:31:02.176718|233.12
221522|2022-03-23 08:31:12.528831|234.8
221523|2022-03-23 08:31:24.665252|235.53
221524|2022-03-23 08:32:00.677398|235.61
221525|2022-03-23 08:32:46.292968|235.68
221526|2022-03-23 08:33:08.064290|233.71
221527|2022-03-23 08:33:19.905184|235.38
221528|2022-03-23 08:33:32.668282|235.22
221529|2022-03-23 08:33:42.987208|237.19
221530|2022-03-23 08:33:53.302518|235.14
221531|2022-03-23 08:34:11.864812|235.54
221532|2022-03-23 08:35:00.794551|235.18
221533|2022-03-23 08:35:12.851588|235.43
221534|2022-03-23 08:35:24.494199|234.89
221535|2022-03-23 08:36:08.627040|234.0
221536|2022-03-23 08:36:26.548283|234.07
221537|2022-03-23 08:36:37.919230|234.37
221538|2022-03-23 08:36:53.891550|233.26
221539|2022-03-23 08:37:05.871792|232.21
221540|2022-03-23 08:37:16.213301|233.36
221541|2022-03-23 08:37:26.492268|232.75
221542|2022-03-23 08:37:38.375853|232.05
221543|2022-03-23 08:37:51.172715|231.55
221544|2022-03-23 08:38:01.526706|231.17
221545|2022-03-23 08:38:11.801171|227.22
221546|2022-03-23 08:38:24.068011|229.62
221547|2022-03-23 08:38:38.054712|234.2
221548|2022-03-23 08:39:31.895501|233.53
221549|2022-03-23 08:39:57.695307|232.52
221550|2022-03-23 08:40:15.447165|232.47
221551|2022-03-23 08:40:32.277798|231.74
221552|2022-03-23 08:40:47.157171|227.93
221553|2022-03-23 08:41:01.781600|228.99
221554|2022-03-23 08:41:16.873229|227.44
221555|2022-03-23 08:41:31.643615|227.77
221556|2022-03-23 08:41:46.544188|228.7
221557|2022-03-23 08:42:01.975447|228.88
221558|2022-03-23 08:42:17.085603|231.54
221559|2022-03-23 08:42:31.508930|234.36
221560|2022-03-23 08:42:46.469516|232.06
221561|2022-03-23 08:43:01.784301|233.54
221562|2022-03-23 08:43:17.072994|236.22
221563|2022-03-23 08:43:32.083951|234.38
221564|2022-03-23 08:43:46.879787|231.8
221565|2022-03-23 08:44:01.790349|232.41
221566|2022-03-23 08:44:16.456424|230.83
221567|2022-03-23 08:44:31.697554|233.99
221568|2022-03-23 08:44:47.039657|233.75
221569|2022-03-23 08:45:01.629946|235.14
221570|2022-03-23 08:45:16.809688|233.25
221571|2022-03-23 08:45:27.112430|236.15
221572|2022-03-23 08:45:37.395134|232.63
221573|2022-03-23 08:45:47.686103|236.23
221574|2022-03-23 08:45:57.972811|234.02
221575|2022-03-23 08:46:10.114894|231.36
221576|2022-03-23 08:46:20.400897|234.35
221577|2022-03-23 08:46:30.727899|233.14
221578|2022-03-23 08:46:41.053248|232.51
221579|2022-03-23 08:46:51.342453|233.96
221580|2022-03-23 08:47:01.678473|232.56
221581|2022-03-23 08:47:12.005399|233.97
221582|2022-03-23 08:47:22.328787|235.98
221583|2022-03-23 08:47:32.646055|236.2
221584|2022-03-23 08:47:42.931063|231.4
221585|2022-03-23 08:47:53.219864|230.0
221586|2022-03-23 08:48:03.514733|229.96
221587|2022-03-23 08:48:13.845995|234.95
221588|2022-03-23 08:48:24.161308|234.31
221589|2022-03-23 08:48:34.482497|235.35
221590|2022-03-23 08:48:44.766851|233.61
221591|2022-03-23 08:48:55.082453|233.71
221592|2022-03-23 08:49:11.189965|235.9
221593|2022-03-23 08:49:21.491651|236.69
221594|2022-03-23 08:49:31.798768|237.88
221595|2022-03-23 08:49:42.122480|235.98
221596|2022-03-23 08:49:52.418405|239.38
221597|2022-03-23 08:50:02.705598|236.95
221598|2022-03-23 08:50:13.016531|237.83
221599|2022-03-23 08:50:23.308529|236.35
221600|2022-03-23 08:50:33.640350|237.57
221601|2022-03-23 08:50:43.951558|237.66
221602|2022-03-23 08:50:54.249970|237.69
221603|2022-03-23 08:51:04.578586|236.99
221604|2022-03-23 08:51:16.987639|238.63
221605|2022-03-23 08:51:27.286887|241.71
221606|2022-03-23 08:51:37.578347|237.64
221607|2022-03-23 08:51:47.852868|241.27
221608|2022-03-23 08:51:58.140011|239.93
221609|2022-03-23 08:52:10.474792|240.78
221610|2022-03-23 08:52:20.757376|240.19
221611|2022-03-23 08:52:31.076549|240.7
221612|2022-03-23 08:52:41.362319|241.28
221613|2022-03-23 08:52:51.701484|242.39
221614|2022-03-23 08:53:01.980319|241.38
221615|2022-03-23 08:53:12.310085|240.94
221616|2022-03-23 08:53:22.626300|239.68
221617|2022-03-23 08:53:32.954818|241.36
221618|2022-03-23 08:53:43.229267|238.16
221619|2022-03-23 08:53:53.513475|239.37
221620|2022-03-23 08:54:09.960105|242.23
221621|2022-03-23 08:54:20.250239|242.16
221622|2022-03-23 08:54:30.572300|240.35
221623|2022-03-23 08:54:40.886182|242.55
221624|2022-03-23 08:54:51.174385|241.62
221625|2022-03-23 08:55:01.527550|238.18
221626|2022-03-23 08:55:11.823697|238.91
221627|2022-03-23 08:55:22.313078|240.06
221628|2022-03-23 08:55:32.628169|241.03
221629|2022-03-23 08:55:42.928062|239.88
221630|2022-03-23 08:55:53.251957|240.02
221631|2022-03-23 08:56:09.494220|241.53
221632|2022-03-23 08:56:19.913754|238.12
221633|2022-03-23 08:56:30.192585|238.8
221634|2022-03-23 08:56:40.518266|242.06
221635|2022-03-23 08:56:50.804579|241.59
221636|2022-03-23 08:57:01.144732|241.47
221637|2022-03-23 08:57:12.577769|242.07
221638|2022-03-23 08:57:22.898009|240.25
221639|2022-03-23 08:57:33.241188|241.02
221640|2022-03-23 08:57:43.565434|242.64
221641|2022-03-23 08:57:53.841595|240.16
221642|2022-03-23 08:58:04.119484|239.37
221643|2022-03-23 08:58:14.410184|239.61
221644|2022-03-23 08:58:25.490278|242.89
221645|2022-03-23 08:58:35.771887|242.74
221646|2022-03-23 08:58:46.080057|239.4
221647|2022-03-23 08:58:56.363236|242.78
221648|2022-03-23 08:59:08.622582|242.2
221649|2022-03-23 08:59:18.953163|238.93
221650|2022-03-23 08:59:29.239038|241.47
221651|2022-03-23 08:59:39.570542|240.22
221652|2022-03-23 08:59:49.878068|239.12
221653|2022-03-23 09:00:00.203349|241.88
221654|2022-03-23 09:00:10.489148|242.22
221655|2022-03-23 09:00:20.767271|237.02
221656|2022-03-23 09:00:31.093296|239.62
221657|2022-03-23 09:00:41.381405|241.56
221658|2022-03-23 09:00:51.698565|240.58
221659|2022-03-23 09:01:01.979103|239.88
221660|2022-03-23 09:01:12.300760|239.97
221661|2022-03-23 09:01:22.625785|240.97
221662|2022-03-23 09:01:32.945467|239.05
221663|2022-03-23 09:01:43.269816|238.84
221664|2022-03-23 09:01:53.582686|241.5
221665|2022-03-23 09:02:08.832874|241.26
221666|2022-03-23 09:02:19.162352|242.9
221667|2022-03-23 09:02:29.486037|237.25
221668|2022-03-23 09:02:39.815679|238.96
221669|2022-03-23 09:02:50.142649|240.48
221670|2022-03-23 09:03:00.436487|237.16
221671|2022-03-23 09:03:10.761514|238.95
221672|2022-03-23 09:03:21.071955|242.4
221673|2022-03-23 09:03:31.349250|240.58
221674|2022-03-23 09:03:41.701157|242.7
221675|2022-03-23 09:03:51.982009|242.86
221676|2022-03-23 09:04:02.264601|240.27
221677|2022-03-23 09:04:12.568010|240.02
221678|2022-03-23 09:04:22.895593|241.56
221679|2022-03-23 09:04:33.195978|242.12
221680|2022-03-23 09:04:43.536890|242.99
221681|2022-03-23 09:04:53.827638|241.9
221682|2022-03-23 09:05:08.621697|239.11
221683|2022-03-23 09:05:18.905773|241.35
221684|2022-03-23 09:05:29.214875|240.41
221685|2022-03-23 09:05:39.527611|239.57
221686|2022-03-23 09:05:49.843797|238.12
221687|2022-03-23 09:06:00.141848|238.82
221688|2022-03-23 09:06:10.463252|241.17
221689|2022-03-23 09:06:21.317547|239.89
221690|2022-03-23 09:06:31.636043|238.85
221691|2022-03-23 09:06:41.985193|239.66
221692|2022-03-23 09:06:52.267225|239.58
221693|2022-03-23 09:07:02.588819|243.0
221694|2022-03-23 09:07:16.130299|241.04
221695|2022-03-23 09:07:26.404450|236.65
221696|2022-03-23 09:07:36.725877|242.23
221697|2022-03-23 09:07:47.085265|241.08
221698|2022-03-23 09:07:57.470750|242.2
221699|2022-03-23 09:08:12.828923|240.26
221700|2022-03-23 09:08:23.211681|242.27
221701|2022-03-23 09:08:33.558867|242.12
221702|2022-03-23 09:08:43.885047|241.59
221703|2022-03-23 09:08:54.242664|241.96
221704|2022-03-23 09:09:04.596579|240.26
221705|2022-03-23 09:09:15.838792|242.18
221706|2022-03-23 09:09:26.198018|242.52
221707|2022-03-23 09:09:36.533728|242.46
221708|2022-03-23 09:09:46.882959|243.61
221709|2022-03-23 09:09:57.265166|242.56
221710|2022-03-23 09:10:14.878265|241.07
221711|2022-03-23 09:10:25.228116|238.18
221712|2022-03-23 09:10:35.572799|238.79
221713|2022-03-23 09:10:45.887122|242.51
221714|2022-03-23 09:10:56.166402|243.48
221715|2022-03-23 09:11:11.600169|243.56
221716|2022-03-23 09:11:21.875800|241.99
221717|2022-03-23 09:11:32.195881|240.9
221718|2022-03-23 09:11:42.482140|242.36
221719|2022-03-23 09:11:52.774749|238.49
221720|2022-03-23 09:12:03.050786|244.42
221721|2022-03-23 09:12:13.384819|241.63
221722|2022-03-23 09:12:23.699770|238.61
221723|2022-03-23 09:12:34.020437|240.99
221724|2022-03-23 09:12:44.302949|241.04
221725|2022-03-23 09:12:54.609562|241.65
221726|2022-03-23 09:13:04.926121|241.36
221727|2022-03-23 09:13:20.313292|242.29
221728|2022-03-23 09:13:30.677382|241.13
221729|2022-03-23 09:13:41.155962|239.9
221730|2022-03-23 09:13:51.568810|241.4
221731|2022-03-23 09:14:01.958379|241.13
221732|2022-03-23 09:14:16.783782|241.59
221733|2022-03-23 09:14:27.176353|242.46
221734|2022-03-23 09:14:37.586889|241.66
221735|2022-03-23 09:14:48.035621|242.72
221736|2022-03-23 09:14:58.432423|239.19
221737|2022-03-23 09:15:08.812921|242.48
221738|2022-03-23 09:15:19.228132|242.35
221739|2022-03-23 09:15:33.088485|241.16
221740|2022-03-23 09:15:43.461496|241.41
221741|2022-03-23 09:15:53.829068|240.13
221742|2022-03-23 09:16:04.200552|243.07
221743|2022-03-23 09:16:25.372528|242.07
221744|2022-03-23 09:16:35.742537|240.83
221745|2022-03-23 09:16:46.116291|242.02
221746|2022-03-23 09:16:56.528103|238.52
221747|2022-03-23 09:17:06.937614|242.4
221748|2022-03-23 09:17:29.214212|241.48
221749|2022-03-23 09:17:39.569380|240.06
221750|2022-03-23 09:17:49.925051|243.7
221751|2022-03-23 09:18:00.314546|242.92
221752|2022-03-23 09:18:10.710766|241.34
221753|2022-03-23 09:18:28.221687|240.72
221754|2022-03-23 09:18:38.675974|240.97
221755|2022-03-23 09:18:49.072407|236.89
221756|2022-03-23 09:18:59.466969|242.03
221757|2022-03-23 09:19:09.834186|242.48
221758|2022-03-23 09:19:27.438190|242.76
221759|2022-03-23 09:19:37.865399|238.28
221760|2022-03-23 09:19:48.300233|238.22
221761|2022-03-23 09:19:58.738318|242.16
221762|2022-03-23 09:20:15.988157|243.09
221763|2022-03-23 09:20:26.446625|240.53
221764|2022-03-23 09:20:36.881158|242.83
221765|2022-03-23 09:20:47.325753|240.72
221766|2022-03-23 09:20:57.745752|242.54
221767|2022-03-23 09:21:08.130422|241.8
221768|2022-03-23 09:21:18.542783|238.78
221769|2022-03-23 09:21:34.982471|240.7
221770|2022-03-23 09:21:45.350427|240.41
221771|2022-03-23 09:21:55.703471|241.71
221772|2022-03-23 09:22:11.914038|241.96
221773|2022-03-23 09:22:22.240699|243.47
221774|2022-03-23 09:22:32.563251|243.98
221775|2022-03-23 09:22:42.895765|239.85
221776|2022-03-23 09:22:53.188993|242.17
221777|2022-03-23 09:23:08.401206|240.48
221778|2022-03-23 09:23:18.683527|239.37
221779|2022-03-23 09:23:29.012436|240.76
221780|2022-03-23 09:23:39.332266|243.1
221781|2022-03-23 09:23:49.611961|242.07
221782|2022-03-23 09:23:59.891490|241.93
221783|2022-03-23 09:24:14.154701|241.06
221784|2022-03-23 09:24:24.431753|241.35
221785|2022-03-23 09:24:34.764448|239.5
221786|2022-03-23 09:24:45.042807|239.52
221787|2022-03-23 09:24:55.352967|240.74
221788|2022-03-23 09:25:08.283903|241.21
221789|2022-03-23 09:25:18.619782|241.23
221790|2022-03-23 09:25:28.935174|240.63
221791|2022-03-23 09:25:39.262783|243.34
221792|2022-03-23 09:25:49.569143|241.38
221793|2022-03-23 09:25:59.881144|241.61
221794|2022-03-23 09:26:11.287888|244.01
221795|2022-03-23 09:26:21.686453|240.53
221796|2022-03-23 09:26:32.061271|242.87
221797|2022-03-23 09:26:42.387946|243.43
221798|2022-03-23 09:26:52.685842|242.7
221799|2022-03-23 09:27:02.996185|236.82
221800|2022-03-23 09:27:13.308467|241.49
221801|2022-03-23 09:27:27.928684|243.16
221802|2022-03-23 09:27:38.214097|240.7
221803|2022-03-23 09:27:48.500590|242.48
221804|2022-03-23 09:27:58.789643|239.35
221805|2022-03-23 09:28:09.120558|241.18
221806|2022-03-23 09:28:26.217733|241.06
221807|2022-03-23 09:28:36.768353|240.59
221808|2022-03-23 09:28:47.115654|241.28
221809|2022-03-23 09:28:57.498655|241.62
221810|2022-03-23 09:29:08.336449|241.33
221811|2022-03-23 09:29:31.780031|242.99
221812|2022-03-23 09:29:42.170203|239.42
221813|2022-03-23 09:29:52.483468|239.76
221814|2022-03-23 09:30:02.845464|242.29
221815|2022-03-23 09:30:20.455335|243.39
221816|2022-03-23 09:30:30.753419|243.37
221817|2022-03-23 09:30:41.042095|238.27
221818|2022-03-23 09:30:51.330290|241.89
221819|2022-03-23 09:31:01.661853|239.16
221820|2022-03-23 09:31:11.959550|239.87
221821|2022-03-23 09:31:23.987483|242.39
221822|2022-03-23 09:31:34.297554|242.24
221823|2022-03-23 09:31:44.801945|238.02
221824|2022-03-23 09:31:55.247081|242.28
221825|2022-03-23 09:32:10.864869|239.89
221826|2022-03-23 09:32:21.194620|244.81
221827|2022-03-23 09:32:31.526516|240.02
221828|2022-03-23 09:32:42.149504|239.03
221829|2022-03-23 09:32:52.474664|241.45
221830|2022-03-23 09:33:02.756448|238.51
221831|2022-03-23 09:33:13.165950|241.88
221832|2022-03-23 09:33:26.251531|239.12
221833|2022-03-23 09:33:36.538779|242.92
221834|2022-03-23 09:33:46.820857|243.6
221835|2022-03-23 09:33:57.103235|241.56
221836|2022-03-23 09:34:07.386125|242.41
221837|2022-03-23 09:34:20.629262|242.32
221838|2022-03-23 09:34:30.961169|242.95
221839|2022-03-23 09:34:41.241653|240.78
221840|2022-03-23 09:34:51.536036|241.69
221841|2022-03-23 09:35:01.874561|241.15
221842|2022-03-23 09:35:12.188716|239.53
221843|2022-03-23 09:35:24.873743|241.86
221844|2022-03-23 09:35:37.022633|239.89
221845|2022-03-23 09:35:47.344370|240.13
221846|2022-03-23 09:35:57.685950|242.47
221847|2022-03-23 09:36:08.013554|243.02
221848|2022-03-23 09:36:20.607524|240.52
221849|2022-03-23 09:36:30.908793|242.04
221850|2022-03-23 09:36:41.239174|241.58
221851|2022-03-23 09:36:51.564411|243.16
221852|2022-03-23 09:37:01.854305|240.7
221853|2022-03-23 09:37:12.166257|240.71
221854|2022-03-23 09:37:22.445580|239.24
221855|2022-03-23 09:37:32.775580|239.93
I'm trying to use sqlalchemy (completely new for me) to select minimum value for every 15-minute period. Honestly I have no idea how to do this in plain sql not even speaking about sqlalchemy.
Can anybody give me some hints?
EDIT 1:
I think I managed to compose SQL query
SELECT timestamp, min(value) FROM pathloss where datetime(timestamp) >=datetime('now', '-4 Hours') group by strftime('%s',timestamp) /900;
but I still will appreciate any help with transforming it to sqlalchemy
EDIT 2:
added sql alchemy model setup
class Pathloss(db.Model):
__tablename__ = 'pathloss'
id = Column(Integer, primary_key=True)
timestamp = Column(DateTime, default=datetime.now)
value = Column(Float)
def __repr__(self):
return '<Pathloss timestamp={}, value={}>'.format(self.timestamp, self.value)
Related
Check constraint on django
I have two models class Order(models.Model): ord_number:str ord_date: date ord_ref: str Null=True ord_qty: Decimal ord_desc: str timestamp: datetime created: datetime Class OrderLines(models.Model): order: FK(Order) driver: FK(Driver) truck: FK(Truck) ord_qty: Decimal loaded_quantity: Decimal loading_date: date My schemas on posting will be like this OrderCreateBase = create_schema(Order, exclude=("id", "timestmap", "created")) OrderLineCreateBase = create_schema(OrderLines, exclude=("id)) class OrderCreateSchema(OrderCreateBase): order_lines = List[OrderLineCreateBase] Json data to be used on creating like this "ord_number: "AB123", "order_date: Date(01/01/2021), "ord_ref" Null, "ord_qty": Decimal("10_000"), "ord_desc" "Sample Order", "order_lines"[ {"order_id": 1, "driver_id: 23, "truck_id": 12, "ord_qty": Decimal("10_000"), "loaded_quantity": Decimal("8_000") <----#, "loading_date": Date(01/01/2021)}, {"order_id": 1, "driver_id: 13, "truck_id": 17, "ord_qty": Decimal("10_000"), "loaded_quantity": Decimal("9_000") <----#, "loading_date": Date(01/01/2021)}] } I want check constraint that will ensure total loaded quantity(loaded_quantity) per order will be <= to ordered_quantity (ord_qty) currently my create endpoint is like this #router.post( "/ilr/{ilr_id}/instrunctions", response={HTTPStatus.CREATED: Message, HTTPStatus.BAD_REQUEST: Message}, tags=["ilrs instructions"], summary=_("create loading request instructions"), description=_("API to create ilr instructions lines"), url_name="create_ilr_instructions", ) #transaction.atomic() def create_ilr_instrunctions(request, ilr_id: int, payload: ILRLineCreateSchema): data = payload.dict() ilr = get_object_or_404(ILR, id=ilr_id, status=0) data["request"] = ilr data["order_date"] = ilr.ilr_date # get existing lines total volume total_volume = ILRLine.objects.filter(request=ilr).aggregate(total_volume=Sum("quantity")) if ilr.quantity < total_volume.get("total_volume") + data.get("quantity"): <-----# return HTTPStatus.BAD_REQUEST, { "detail": [{"msg": f"Total loading quantities will be higher than ordered quantity"}] } try: with transaction.atomic(): ILRLine.objects.create(**data) ..... How can I force it on database level using check constraint? I don't want to override model save
You can try CheckConstraint.check from django.db.models import CheckConstraint, Q, F class OrderLines(models.Model): ... class Meta: constraints = [ CheckConstraint( check=Q(loaded_quantity__lte=F('ord_qty')), name='quantity_check' ) ]
Django orm: moving selected records from a column to another and save to database
I have the following list: list = [ "WP1788/1", "WP1810/1", "WP1810/2", "WP1812/1", "WP1815/1", "WP1818/1", "WP1823/1", "WP1827/1", "WP1828/1", "WP1828/2", "WP1828/3", "WP1828/4", "WP1828/5", "WP1828/6", "WP1837/1", "WP1840/1", "WP1841/1", "WP1855/1", "WP1860/1", "WP1861/1", "WP1863/1", "WP1872/1", "WP1873/1", "WP1873/2", "WP1879/1", "WP1884/1", "WP1888/1", "WP1895/1", "WP1895/2", "WP1906/1", "WP1906/2", "WP1908/1", "WP1908/2", "WP1909/1", "WP1909/2", "WP1913/1", "WP1918/1", "WP1919/1", "WP1919/2", "WP1919/3", "WP1922/1", "WP1928/1", "WP1928/3", "WP1928/4", "WP1928/5", "WP1928/6", "WP1944/1", "WP1944/2", "WP1945/1", "WP1946/1", "WP1947/1", "WP1955/1", "WP1962/1", "WP1965/1", "WP1965/2", "WP1967/1", "WP1969/1", "WP1977/1", "WP1988/1", "WP1991/1", "WP1991/5", "WP1995/1", "WP2002/1", "WP2012/1", "WP2015/1", "WP2017/1", "WP2021/1", "WP2022/1", "WP2024/1", "WP2033/1", "WP2033/2", "WP2044/1", "WP2050/1", "WP1585/1", "WP1585/2", "WP1585/4", "WP1585/5", "WP1585/6", "WP1585/7", "WP1585/8", "WP1585/9", "WP1624/103", "WP1624/105", "WP1624/108", "WP1624/109", "WP1624/118", "WP1624/119", "WP1624/120", "WP1624/121", "WP1624/123", "WP1624/129", "WP1624/130", "WP1624/137", "WP1624/145", "WP1624/165", "WP1624/83", "WP1624/85", "WP1624/91", "WP1624/93", "WP1670/1", "WP1708/10", "WP1708/12", "WP1708/13", "WP1708/14", "WP1708/15", "WP1708/17", "WP1708/20", "WP1708/22", "WP1708/26", "WP1708/27", "WP1708/28", "WP1779/26", "WP1838/1", "WP1844/1", "WP1876/1", "WP1882/1", ] I would like to select the wps (Wp model) in the database with ID in list, something like: wps_selected = Wp.objects.filter(ID in list) and then copy the value from the column working_hours to the column non_profitable and save it to database. Is that possible using ORM. I know how to do it in SQL but I am still a bit comfuased about using ORM
You can do that in a simple for loop: wps_selected = Wp.objects.filter(ID__in=list) for wps in wps_selected: wps.non_profitable = wps.working_hours wps.save()
You can update in bulk with: from django.db.models import F Wp.objects.filter( ID__in=mylist ).update(non_profitable=F('working_hours')) Note: Please do not name a variable list, it overrides the reference to the list builtin function [Python-doc]. Use for example mylist.
Change a series I have been given
from sqlalchemy import Column, Integer, String, Boolean, Float, DateTime, ForeignKey from sqlalchemy.orm import relationship, reconstructor from app import utils import datetime import pandas from base import Base from series import Series class ConstantSeries(Series): __tablename__ = 'constantseries' # ID Primary Key (linked to parent) id = Column(Integer, ForeignKey('series.id'), primary_key=True) # The value of this series to be returned for all periods. value = Column(Float) __mapper_args__ = {'polymorphic_identity': 'constantseries'} def GetData(self, scenario, periods): """Get data values for the specified periods. """ return pandas.Series(data=[self.value]*len(periods), index=periods) I have been given the code above but I want to be able to change it so that I can have a series that has different values depending on what day it is. For example if it is a weekday I have a value of 100 and for a weekend have a value of 200
Can you tell me how you are calling the above code and what it currently returns? It seems that the GetData function just returns a data structure, i.e. it returns the structure of a Column for every period, but no actual data. What's not shown here is how the actual data in the Columns is populated and accessed. You could loop through your periods and build the Series data according to the day e.g if periods contained ['mon','tue','wed','thu','fri','sat','sun'] def GetData(self, scenario, periods): mydata = {} for p in periods: if p in ['sat','sun']: e[p] = 200 else: e[p] = 100 return pandas.Series(mydata, index=periods) Then calling GetData should return something like 'mon' 100 'tue' 100 ... 'sat' 200 'sun' 200 but thats not the structure you want and i dont think its how the Getdata function is being used.
def GetData(self, scenario, periods): rtn = {} for timest, val in cap.GetData(base_scenario,utils.enumerate_periods(start,end,'H','CET')).iteritems(): if timest.weekday_name in ['Saturday', 'Sunday']: rtn[timest.weekday_name] = (0.72 * val) #0.46*2574 else: rtn[timest.weekday_name] = (1.0 * val) return [rtn]
#Define ConstantSeries class #I have made no changes here, just what you already had class ConstantSeries(Series): __tablename__ = 'constantseries' # ID Primary Key (linked to parent) id = Column(Integer, ForeignKey('series.id'), primary_key=True) # The value of this series to be returned for all periods. value = Column(Float) __mapper_args__ = {'polymorphic_identity': 'constantseries'} def GetData(self, scenario, periods): """Get data values for the specified periods. """ return pandas.Series(data=[self.value]*len(periods), index=periods) #End of class definition #Define new special Cap2Series class class Cap2Series(Series): #I'm not sure how tablename is used so be aware, might need to put constantseries as before __tablename__ = 'cap2series' # ID Primary Key (linked to parent) id = Column(Integer, ForeignKey('series.id'), primary_key=True) # The value of this series to be returned for all periods. value = Column(Float) #Same as above, I'm not sure how this is used so be aware, might need to put constantseries as before __mapper_args__ = {'polymorphic_identity': 'cap2series'} #Define GetData method of our new special class def GetData(self, scenario, periods): #instantiate new ConstantSeries instance called cap when you call GetData cap = (ConstantSeries(value=self.value)) rtn = {} for timest, val in cap.GetData(scenario, periods).iteritems(): if timest.weekday_name in ['Saturday', 'Sunday']: rtn[timest.weekday_name] = (0.72 * val) #0.46*2574 else: rtn[timest.weekday_name] = (1.0 * val) return pandas.Series(data=rtn, index=periods) #End of class definition #Instantiate new Cap2Series instance called cap2 cap2 = Cap2Series(1647) #Call GetData method of cap2 instance cap2.GetData(base_scenario, utils.enumerate_periods(start,end,'H','CET')).plot() #Is this something like what you're trying to do?
how works createQueryBuilder and leftJoin?
I dont get how to make it work. I have: a table partner with fields id and name a table partner_address with two fields: id_partner and id_address a table address with fields id and external key id_town which references town(id) a table town with fields id, a name, and postal_code I want to select all partners that are in towns with specific postal_code This query works: SELECT p.nom, v.nom FROM partner p JOIN partner_address pa ON pa.id_partner=p.id JOIN address a ON pa.id_address = a.id JOIN town t ON a.id_town=t.id WHERE t.postal_code='13480'; Now I want to "translate" it into Doctrine 2 full syntax, following the documentation. So I've made a custom repository: src/Society/Bundle/MyProjectBundle/Repository/PartnerRepository.php In this repository, I'm trying to create the corresponding function: <?php namespace HQF\Bundle\PizzasBundle\Repository; use Doctrine\ORM\EntityRepository; class PartenaireRepository extends EntityRepository { /** * Get all active partners from a given postal code. */ public function findAllActiveByCp($cp) { return $this->createQueryBuilder('p') ->where('p.dateVFin IS NULL') ->andWhere('p.cp=:cp') ->addOrderBy('p.cp', 'DESC') ->setParameter('cp', $cp); } } Nota: the query in the code is not the right one but this code works in another custom repository I've made, so I'm trying to start from this code. I'm trying something like this but it doesn't work: public function findAllActiveByCp($cp) { $qb = $this->createQueryBuilder('p'); return $qb ->leftJoin('partner_address pa ON pa.id_partner=p.id') ->leftJoin('address a ON pa.id_address = a.id') ->leftJoin('town t ON a.id_ville=t.id') ->where('p.dateVFin IS NULL') ->andWhere('t.cp=:cp') ->addOrderBy('t.cp', 'DESC') ->setParameter('cp', $cp); } I get this error: Warning: Missing argument 2 for Doctrine\ORM\QueryBuilder::leftJoin(), called in /blabla/Repository/PartenaireRepository.php on line 18 and defined in /blabla/symfony/vendor/doctrine/orm/lib/Doctrine/ORM/QueryBuilder.php line 767
You have to join only properties, that the selected entity have. In first parameter of join() or leftJoin() or xxxJoin() you pass the attribute name related to selected object, and in the second - alias for joined entity. Try similar to this: $q = $this->em()->createQueryBuilder(); $q->select(['item', 'itemContact']) ->from('ModuleAdmin\Entity\CustomerEntity', 'item') ->leftJoin('item.contacts', 'itemContact') ->andWhere($q->expr()->like('item.name', ':customerNameStart')); Of course, the CustomerEntity contains OneToMany relation in field contacts. Remember, that in select statement you have to select the root entity (in my example CustomerEntity aliased as item). Edit by Olivier Pons to add how I found out the solution, and to mark this answer as valid, because it put me on the right track, thank you Adam! In the file PartenaireRepository.php I've used the createQueryBuilder('p') properly. Here's how to make two joins in a row, using createQueryBuilder(): class PartenaireRepository extends EntityRepository { /** * Retrieval of all partners given for a given postal code. */ public function findAllActiveByCp($cp) { return $this->createQueryBuilder('p') ->leftJoin('p.adresses', 'a') ->leftJoin('a.ville', 'v') ->where('v.cp=:cp') ->setParameter('cp', $cp); ... blabla } }
I believe for what you're doing, you will need to provide four arguments to the leftJoin method. ->leftJoin('partner_address', 'pa', 'ON', 'pa.id_partner = p.id') So your query builder chain should look like this public function findAllActiveByCp($cp) { $qb = $this->createQueryBuilder('p'); return $qb ->leftJoin('partner_address', 'pa', 'ON', 'pa.id_partner = p.id') ->leftJoin('address', 'a', 'ON', 'pa.id_address = a.id') ->leftJoin('town', 't', 'ON', 'a.id_ville = t.id') ->where('p.dateVFin IS NULL') ->andWhere('t.cp=:cp') ->addOrderBy('t.cp', 'DESC') ->setParameter('cp', $cp) ; }
Testing NHibernate with SQLite "No Such Table" - schema is generated
I'm trying to use an in-memory SQLite database to test my data layer which is provided by NHibernate. I've read a load of blogs and articles about getting this setup but I'm now very confused as to why it isn't working. The problem - when I run a unit test I get the error 'no such table: Student'. The articles I've read suggest this is because the schema isn't getting generated, or, the connection is being closed between my SchemaExport and query. I've checked everywhere I can think of and can't see how either of these scenarios are occuring. My test output log looks like this: OPEN CONNECTION drop table if exists "Student" drop table if exists "Tutor" create table "Student" ( ID integer, Name TEXT, DoB DATETIME, TutorId INTEGER, primary key (ID) ) create table "Tutor" ( ID integer, Name TEXT, primary key (ID) ) NHibernate: INSERT INTO "Student" (Name, DoB, TutorId) VALUES (#p0, #p1, #p2); select last_insert_rowid();#p0 = 'Text1', #p1 = 01/12/2010 14:55:05, #p2 = NULL 14:55:05,750 ERROR [TestRunnerThread] AbstractBatcher [(null)]- Could not execute query: INSERT INTO "Student" (Name, DoB, TutorId) VALUES (#p0, #p1, #p2); select last_insert_rowid() System.Data.SQLite.SQLiteException (0x80004005): SQLite error no such table: Student at System.Data.SQLite.SQLite3.Prepare(String strSql, SQLiteStatement previous, String& strRemain) at System.Data.SQLite.SQLiteCommand.BuildNextCommand() at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader() at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd) 14:55:05,781 ERROR [TestRunnerThread] ADOExceptionReporter [(null)]- SQLite error no such table: Student DISPOSE CLOSING CONNECTION Originally I was using my own code for the connection/session management but have moved to the code in this blog post translated to C# and with a couple changes to the DBConfig method and some debug statements to show the state of the connection. private FluentNHibernate.Cfg.Db.IPersistenceConfigurer GetDBConfig() { return SQLiteConfiguration.Standard .ConnectionString((ConnectionStringBuilder cs) => cs.Is(CONNECTION_STRING)) .ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu") .Raw("connection.release_mode", "on_close"); } I added the on_close after reading this My test code looks like this: [Test] public void CanGetStudentById() { using (var scope = new SQLiteDatabaseScope<StudentMapping>()) { using (ISession sess = scope.OpenSession()) { // Arrange var repo = new StudentRepository(); repo.Save(new Student() { Name = "Text1", DoB = DateTime.Now }); // Act var student = repo.GetById(1); // Assert Assert.IsNotNull(student); Assert.AreEqual("Text1", student.Name); } } } What have I overlooked here? Update: I created a copy of the class that connects to an SQLite file DB and it worked fine. So it has to be something to do with the connection being closed.
If you change your test method to the following, does it work? [Test] public void CanGetStudentById() { using (var scope = new SQLiteDatabaseScope<StudentMapping>()) { using (ISession sess = scope.OpenSession()) { // Arrange sess.Save(new Student() { Name = "Text1", DoB = DateTime.Now }); // Act var student = sess.Get<Student>(1); // Assert Assert.IsNotNull(student); Assert.AreEqual("Text1", student.Name); } } } I would hazard to guess that your StudentRepository is opening its own session and hence doesn't see the table.