pudo / dataset

Easy-to-use data handling for SQL data stores with support for implicit table creation, bulk loading, and transactions.
https://dataset.readthedocs.org/
MIT License
4.78k stars 298 forks source link

primary_id cannot be changed after table is dropped #329

Closed luerhard closed 4 years ago

luerhard commented 4 years ago

Hey there, first of all, thank you for this amazing package, I use it every day !

I have found a problem, that I cannot quite explain. If I drop a Table and afterwards create a Table with the same name, I cannot change the primary_id column for this new Table.

If I 'reconnect' (uncomment the commented line in the sourcecode, this script works like expected:

import dataset
print("version: ", dataset.__version__, end="\n\n")

import os
if os.path.exists("test.db"):
    os.remove("test.db")

db = dataset.connect("sqlite:///test.db", row_type=dict)

db.create_table("my_table", primary_id="id")
db["my_table"].insert({"a": 2, "b": 3})
print("After first input: ", list(db["my_table"].all()))

db["my_table"].drop()
print("Tables in DB after drop: ", db.tables)

#db = dataset.connect("sqlite:///test.db", row_type=dict)

db.create_table("my_table", primary_id="custom")
db["my_table"].insert({"a": 20, "b": 30, "custom": 50})
print("After second input: ", list(db["my_table"].all()))

print()
if "custom" in db["my_table"].table.c:
    print("custom_col == primary ->", db["my_table"].table.c.custom.primary_key)
if "id" in db["my_table"].table.c:
    print("id_col == primary ->", db["my_table"].table.c.id.primary_key)

The output of this is with line commented:

version:  1.3.1

After first input:  [{'id': 1, 'a': 2, 'b': 3}]
Tables in DB after drop:  []
After second input:  [{'id': 1, 'a': 20, 'b': 30, 'custom': 50}]

custom_col == primary -> False
id_col == primary -> True

Output fo this with the line uncommented:

version:  1.3.1

After first input:  [{'id': 1, 'a': 2, 'b': 3}]
Tables in DB after drop:  []
After second input:  [{'custom': 50, 'a': 20, 'b': 30}]

custom_col == primary -> True

My Problem is the difference in the line that says "After second input". If i do not 'reconnect' to the DB, there is an additional column there.