{"id":2101,"date":"2014-03-03T13:06:56","date_gmt":"2014-03-03T13:06:56","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2101"},"modified":"2014-03-03T13:06:56","modified_gmt":"2014-03-03T13:06:56","slug":"inspecting-postgres-column-types-sqlalchemy","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/inspecting-postgres-column-types-sqlalchemy\/","title":{"rendered":"Inspecting Postgres column types with SqlAlchemy"},"content":{"rendered":"<p>SQL Alchemy makes it easy to get types out of the database:<\/p>\n<pre lang=\"python\">\nfrom sqlalchemy import *\nengine = create_engine(\n                \"postgresql+pg8000:\/\/postgres:postgres@localhost\/postgres\",\n                isolation_level=\"READ UNCOMMITTED\"\n            )\nc = engine.connect()\n \nmeta = MetaData()\n \nt = Table('table', meta, autoload=True, autoload_with=engine, schema='test')\n\ncolumns = [col for col in t.columns]\n<\/pre>\n<p>And then from there, you can filter the column list down to things you want. <\/p>\n<pre lang=\"python\">\n\nimport sqlalchemy.sql.sqltypes\n\ndef useColumn(c):\n  if (type(c.type) is TIMESTAMP):\n    return False\n\n  if (type(c.type) is VARCHAR):\n    if (c.type.length == 24):\n      return False\n\n  if (type(c.type) is DATE):\n    return False\n\n  if (c.name.startswith(\"internal_\")):\n    return False\n\n  if (c.name == \"dont_use_me\"): \n    return False\n\n  return True\n<\/pre>\n<p>For certain columns, SQL Alchemy will give you an object that is a database specific type (e.g. there is a Postgres namespace), but you can find the exact class name:<\/p>\n<pre lang=\"python\">\ncolumns[0].type.__class__\n\n...sqlalchemy.sql.sqltypes.INTEGER\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SQL Alchemy makes it easy to get types out of the database: from sqlalchemy import * engine = create_engine( &#8220;postgresql+pg8000:\/\/postgres:postgres@localhost\/postgres&#8221;, isolation_level=&#8221;READ UNCOMMITTED&#8221; ) c = engine.connect() meta = MetaData() t = Table(&#8216;table&#8217;, meta, autoload=True, autoload_with=engine, schema=&#8217;test&#8217;) columns = [col for col in t.columns] And then from there, you can filter the column list down to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/inspecting-postgres-column-types-sqlalchemy\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Inspecting Postgres column types with SqlAlchemy&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[157,437,525],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2101"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2101"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2101\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}