0

I want to do a SELECT statement that will get all the data in one table + follow all the foreign keys from that table with a LEFT OUTER JOIN. For example:

`orderitem`
id
name
title_id

`title`
id
name

In the above example, I would be able to use the statement:

SELECT * FROM orderitem LEFT OUTER JOIN title on orderitem.title_id=title.id

Is there a way that I could do this not knowing the table structure? That is, to have a function like the following:

def get_select_statement(table)
    ???

get_select_statement(orderitem)
==> "SELECT * FROM orderitem LEFT OUTER JOIN title on orderitem.title_id=title.id"

How would this be done?

To clarify this question, I think I'm looking for the following information from this function:

  • What are all the column names in the given table?
  • What tables do they reference in a ForeignKey relationship and what is the relationship to be able to join?

In addition, note that not all orderitems will have a title, so doing any sort of INNER JOIN would delete data.

David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

1

In MySQLDB you could retrieve column names by using describe statement:

DESCRIBE table_name;

And all info about foreign keys:

select *
from information_schema.KEY_COLUMN_USAGE
where TABLE_SCHEMA = "schema_name" 
      and TABLE_NAME="table_name" 
      and REFERENCED_TABLE_NAME IS NOT NULL

To evaluate this query and load the result in python you could use SQLAlchemy package, for example.

engine = sqlalchemy.create_engine("mysqldb://user:password@host/db")
res = engine.execute("DESCRIBE table_name;")
columns = [row["Field"] for row in res]

res = engine.execute("{}".format(query_for_foreign_keys))
foreign_keys = [row["COLUMN_NAME"] for row in res]
referenced_column_names = [row["REFERENCED_COLUMN_NAME"] for row in res]
referenced_table_names = [row["REFERENCED_TABLE_NAME"] for row in res]

Then you could generate the query using all the data above

alechkai
  • 41
  • 3