<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>QGIS' virtual fields (expression fields) are not
<a class="moz-txt-link-freetext" href="https://www.postgresql.org/docs/current/ddl-generated-columns.html">https://www.postgresql.org/docs/current/ddl-generated-columns.html</a></p>
<p>They are conceptually similar between QGIS and PostgreSQL, but
they are not propagated from QGIS to the data source as a new
column there. They live only in the QGIS project.</p>
<p>If you want a generated column in your PostgreSQL database, you
need to add it another way. E.g. via an SQL query.</p>
<p>Cheers, Hannes</p>
<p></p>
<div class="moz-cite-prefix">On 26.07.24 12:46, Roland Berger via
QGIS-User wrote:<br>
</div>
<blockquote type="cite"
cite="mid:7738df1f-a73c-4928-9438-f47b273cb013@steinerpartner.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
Hi <br>
<br>
I would like to add a virtual field to a PostGIS vector layer with
PyQGIS with standalone scripts
(<a class="moz-txt-link-freetext"
href="https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/intro.html#python-applications"
moz-do-not-send="true">https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/intro.html#python-applications</a>)<br>
All examples I have found so far with google, chatgpt4 and
JetBrains AI Assistant gave me solutions that do not work. They
only work in the Python Console inside QGIS.<br>
Does anybody know of a solution that works with PyQGIS with
standalone scripts?<br>
See below what I have tried so far in different variations.<br>
<br>
Thanks for any help <br>
Roland<br>
<br>
Example created by chatgpt:<br>
...<br>
<pre>field_name = "virtual_field"</pre>
<pre>expression = '"attribute_name" * 2' # Example expression</pre>
<pre># Create and add the virtual field</pre>
<pre>layer.startEditing()</pre>
<pre>layer.addExpressionField(expression, QgsField(field_name, QVariant.Double))</pre>
<pre>layer.commitChanges()</pre>
...<br>
<br>
<br>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
QGIS-User mailing list
<a class="moz-txt-link-abbreviated" href="mailto:QGIS-User@lists.osgeo.org">QGIS-User@lists.osgeo.org</a>
List info: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/qgis-user">https://lists.osgeo.org/mailman/listinfo/qgis-user</a>
</pre>
</blockquote>
<pre class="moz-signature" cols="72">--
Johannes Kröger / GIS-Entwickler/-Berater
***********************************************************
FOSS Academy Sommerschule: Kompaktkurs zum Aufbau einer GDI
02.-06. September 2024, Präsenzveranstaltung in Bonn
<a class="moz-txt-link-freetext" href="https://www.foss-academy.com/kompaktkurse">https://www.foss-academy.com/kompaktkurse</a>
***********************************************************
WhereGroup GmbH
c/o KK03 GmbH
Lange Reihe 29
20099 Hamburg
Germany
Tel: +49 (0)228 / 90 90 38 - 36
Fax: +49 (0)228 / 90 90 38 - 11
<a class="moz-txt-link-abbreviated" href="mailto:johannes.kroeger@wheregroup.com">johannes.kroeger@wheregroup.com</a>
<a class="moz-txt-link-abbreviated" href="http://www.wheregroup.com">www.wheregroup.com</a>
Geschäftsführer:
Olaf Knopp, Peter Stamm
Amtsgericht Bonn, HRB 9885
-------------------------------
</pre>
</body>
</html>