<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Best practice to set a table's PRIMARY KEY in a MS SQL Enterprise Geodatabase in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/best-practice-to-set-a-table-s-primary-key-in-a-ms/m-p/1499349#M70936</link>
    <description>&lt;P&gt;Using geoaccessor method to_table() and to_featureclass()&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_0-1719604987205.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/108334i963EE50F889879AF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TylerT_0-1719604987205.png" alt="TylerT_0-1719604987205.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;do not set a PRIMARY KEY (PK) in the enterprise geodatabase.&amp;nbsp; A PK is required to reflect tables as orm declarative tables with SQLAlchemy's automap_base.&amp;nbsp; This is why I want to set a PK.&lt;BR /&gt;&lt;BR /&gt;Is there any risk to the geodatabase inner workings to set a PK by passing the following SQL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;ALTER TABLE {tbl} ALTER COLUMN {pk} {dtype} NOT NULL
ALTER TABLE {tbl} ADD PRIMARY KEY({pk})&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This SQL runs without error, and following the PK set, sqlalchemy is much happier.&amp;nbsp; I'm curious if there are any unintended consequences to setting PK with third party tools such as SQL (i.e. outside of ArcGIS API).&lt;BR /&gt;&lt;BR /&gt;If this is NOT recommended, does ArcGIS Pro or Arcpy have a tool to set PK in the EGDB (not to be confused with relationship class PKs), or do you have any other suggestions?&lt;BR /&gt;&lt;BR /&gt;Thank you,&lt;/P&gt;&lt;P&gt;Tyler&lt;/P&gt;</description>
    <pubDate>Fri, 28 Jun 2024 20:30:37 GMT</pubDate>
    <dc:creator>TylerT</dc:creator>
    <dc:date>2024-06-28T20:30:37Z</dc:date>
    <item>
      <title>Best practice to set a table's PRIMARY KEY in a MS SQL Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-to-set-a-table-s-primary-key-in-a-ms/m-p/1499349#M70936</link>
      <description>&lt;P&gt;Using geoaccessor method to_table() and to_featureclass()&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="TylerT_0-1719604987205.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/108334i963EE50F889879AF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="TylerT_0-1719604987205.png" alt="TylerT_0-1719604987205.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;do not set a PRIMARY KEY (PK) in the enterprise geodatabase.&amp;nbsp; A PK is required to reflect tables as orm declarative tables with SQLAlchemy's automap_base.&amp;nbsp; This is why I want to set a PK.&lt;BR /&gt;&lt;BR /&gt;Is there any risk to the geodatabase inner workings to set a PK by passing the following SQL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;ALTER TABLE {tbl} ALTER COLUMN {pk} {dtype} NOT NULL
ALTER TABLE {tbl} ADD PRIMARY KEY({pk})&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This SQL runs without error, and following the PK set, sqlalchemy is much happier.&amp;nbsp; I'm curious if there are any unintended consequences to setting PK with third party tools such as SQL (i.e. outside of ArcGIS API).&lt;BR /&gt;&lt;BR /&gt;If this is NOT recommended, does ArcGIS Pro or Arcpy have a tool to set PK in the EGDB (not to be confused with relationship class PKs), or do you have any other suggestions?&lt;BR /&gt;&lt;BR /&gt;Thank you,&lt;/P&gt;&lt;P&gt;Tyler&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jun 2024 20:30:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-to-set-a-table-s-primary-key-in-a-ms/m-p/1499349#M70936</guid>
      <dc:creator>TylerT</dc:creator>
      <dc:date>2024-06-28T20:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice to set a table's PRIMARY KEY in a MS SQL Enterprise Geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/best-practice-to-set-a-table-s-primary-key-in-a-ms/m-p/1613824#M74174</link>
      <description>&lt;P&gt;Darn! I was wondering about this part "&lt;SPAN&gt;does ArcGIS Pro or Arcpy have a tool to set PK in the EGDB"? I noticed that I can change the configuration keywords to make the index clustered on the OBJECTID but I would love to set it as the PK as well.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;A&lt;/P&gt;</description>
      <pubDate>Mon, 12 May 2025 21:47:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/best-practice-to-set-a-table-s-primary-key-in-a-ms/m-p/1613824#M74174</guid>
      <dc:creator>AndreaB_</dc:creator>
      <dc:date>2025-05-12T21:47:19Z</dc:date>
    </item>
  </channel>
</rss>

