3

I am trying to use SQLAlchemy's ORM layer (via Flask-SQLAlchemy==2.0), and I need to support (ingest and update) some tables that were created with Strings (Varchars) as primary keys. BTW, I am using SQLite as the backend during development, but the production datastore will be Oracle.

The following code was supposed to create the model, and then the test_setup function was supposed to initialize some test data, but I seemingly can only create tables with Integers as primary keys.

# MODEL
class MAG_BAG(db.Model):
    """An entity used for the test_setup"""
    __tablename__ = 'DS_MAG_BAG'
    BAG_ABBREV = db.Column(db.String(18), primary_key=True)
    BAG_NM = db.Column(db.String(128))
    MAGIC_POWER_DESCR = db.Column(db.String(128))
    SORT_ORD = db.Column(db.Integer)

    def __repr__(self):
        return '<MAG_BAG {0}>'.format(self.BAG_NM)

def test_setup():
    """Only used during development or testing as a convenience."""
    db.create_all() # why this no make DS_MAG_BAG?
    #
    if User.query.filter_by(login_id='felix').first() is None:
        u = User.register('felix', 'cat')
        e = Entity.register('Magic Bag', 'DS_MAG_BAG', u.id)
        # u and e populate tables just fine
        # but section below does nothing 
        for b in [('FSH','Magic Fish Sack'),('CANBAG','Canoe Bag'),('ESC', 'Escalator Bag')]:
            print(a_bag)
            a_bag = MAG_BAG(BAG_ABBREV=b[0], BAG_NM=b[1])
            db.session.add(a_bag)
            db.session.commit()

Is creation of tables with PKs of datatypes other than Numeric types possible?

  • The code *defines* a model, but it does not *create* a table. So, how do you tell SQLAlchemy to create the table and what is the output? – Klaus D. Jul 07 '15 at 03:17
  • The first statement in the test_setup function: db.create_all() – Space Pirate Solutions Jul 08 '15 at 00:09
  • I don't use `flask`, but here should be no problem with your setup. I did a quick trial, got your `MAG_BAG` class and created it in a `sqllite` database with `sqlalchemy`: no problem. You get any error message? – lrnzcig Jul 08 '15 at 09:40

0 Answers0