Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column name duplicates unexpected behaviour #3

Closed
philiprekers opened this issue Apr 4, 2022 · 5 comments
Closed

Column name duplicates unexpected behaviour #3

philiprekers opened this issue Apr 4, 2022 · 5 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@philiprekers
Copy link
Contributor

Using fetch_all() with a JOIN in the query, resulting in a result containing the same column name twice, shows only one of both resulting columns in output dictionary.

Example:

Two tables contain a column called name.
We join both tables and the result table now has two columns called name.
With fetch_all() we only get one result set for a column called name (dictionary has unique keys).

Possible fixes:

  • Throw a warning when the result set has a duplicate column name
  • Rename columns in output dictionary when the result set has a duplicate column name
  • If possible use table alias for each of the repeated column names as prefix in output dictionary key, separated by underscore
@severinsimmler
Copy link
Member

What do you think is the expected behavior, @benedikt-budig? I'd say just printing a warning, because it's the user's error if he doesn't rename the column in the SQL query. Not sure yet how SQLAlchemy handles this.

@severinsimmler
Copy link
Member

Could you please provide an example of the JOIN you do where this happens, @Philipduerholt?

@severinsimmler
Copy link
Member

severinsimmler commented Apr 5, 2022

Something likes this?

from databank import Database

db = Database("sqlite:///test.db")

db.execute("CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);")
db.execute("CREATE TABLE b (id INTEGER PRIMARY KEY, name TEXT);")
db.execute("INSERT INTO a (id, name) VALUES (0, 'foo');")
db.execute("INSERT INTO b (id, name) VALUES (0, 'bar');")

If you do now:

>>> db.fetch_one("SELECT a.name, b.name FROM a JOIN b on a.id = b.id;")
{'name': 'bar'}

@severinsimmler
Copy link
Member

SQLAlchemy returns a tuple ("foo", "bar"), but we transform it to a dictionary with the same key.

@severinsimmler
Copy link
Member

Closing this as this is actually expected behavior. If there are duplicate names, one has to do something like:

>>> db.fetch_one("SELECT a.name AS a_name, b.name AS b_name FROM a JOIN b on a.id = b.id;")
{'a_name': 'foo', 'b_name': 'bar'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants