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

SQL: fix meta/has_table #6627

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ def __init__(self, engine, meta=None):
meta = MetaData(self.engine)
meta.reflect(self.engine)

self.meta = meta
self._meta = meta

def execute(self, *args, **kwargs):
"""Simple passthrough to SQLAlchemy engine"""
Expand Down Expand Up @@ -651,6 +651,12 @@ def to_sql(self, frame, name, if_exists='fail', index=True):
name, self, frame=frame, index=index, if_exists=if_exists)
table.insert()

@property
def meta(self):
self._meta.clear()
self._meta.reflect()
return self._meta

@property
def tables(self):
return self.meta.tables
Expand Down
29 changes: 29 additions & 0 deletions pandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ def test_tquery(self):
row = iris_results[0]
tm.equalContents(row, [5.1, 3.5, 1.4, 0.2, 'Iris-setosa'])

def test_has_table(self):
sql.to_sql(self.test_frame1, 'test_frame_has_table', self.conn, flavor='sqlite')

self.assertTrue(
sql.has_table('test_frame_has_table', self.conn, flavor='sqlite'),
"Table not found with has_table but exists")

self.drop_table('test_frame_has_table')

self.assertFalse(
sql.has_table('test_frame_has_table', self.conn, flavor='sqlite'),
"Table found with has_table but does not exist")

def test_date_parsing(self):
""" Test date parsing in read_sql """
# No Parsing
Expand Down Expand Up @@ -475,6 +488,22 @@ def test_drop_table(self):
self.assertFalse(
temp_conn.has_table('temp_frame'), 'Table not deleted from DB')

def test_has_table(self):
temp_frame = DataFrame(
{'one': [1., 2., 3., 4.], 'two': [4., 3., 2., 1.]})

self.pandasSQL.to_sql(temp_frame, 'test_frame_has_table')

self.assertTrue(
self.pandasSQL.has_table('test_frame_has_table'),
"Table not found with has_table but exists")

self.drop_table('test_frame_has_table')

self.assertFalse(
self.pandasSQL.has_table('test_frame_has_table'),
"Table found with has_table but does not exist")

def test_roundtrip(self):
self._roundtrip()

Expand Down