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

Unique and Not Null #266

Closed patarapolw closed 4 years ago

patarapolw commented 6 years ago

I would like to see these two, at least, in a schema-less NoSQL. How do I specify it is dataset?

I managed to create such in TinyDB, but the main problems of TinyDB are:

BTW, does dataset support blob?

Another interesting thing is ForeignKey, but I believe it will never be supported, anyway.

patarapolw commented 6 years ago

I have found an answer, by using SQLAlchemy, minimally.

import dataset
import sqlalchemy as sa

db = dataset.connect('sqlite:///' + db_file)

if not os.path.exists(db_file):
    sa.Table(
        'patient', db.metadata,
        sa.Column('patient_id', sa.Integer, primary_key=True)
    ).create()
    sa.Table(
        'patho_report', db.metadata,
        sa.Column('id', sa.Integer, primary_key=True, autoincrement=True),
        sa.Column('patho_id', sa.String, nullable=False, unique=True),
        sa.Column('patient_id', sa.Integer, sa.ForeignKey('patient.patient_id')),
        sa.Column('received', sa.DateTime)
    ).create()
pudo commented 4 years ago

You can now do:

import dataset

db = dataset.connect('sqlite:///' + db_file)
report = db['patho_report']
report.create_column('patho_id', nullable=False, unique=True)

Foreign keys are, I think, going to remain out of scope unless anyone comes up with a super Pythonic way to express them.