0

I am using flask-sqlalchemy in my application and have created below models. I am creating many to many relationship for Users and Subscription table, and tried to create helper table as User_Subscription. After creating this model, when i run db.create_all() in command line, it is creating only 3 tables in database (tables having db.Model) but not creating helper table (User_Subscription) at all. It is not giving any error either.

Could someone please advise what's going wrong here?

I already searched a lot on google and stackoverflow, but I can't find answer where anyone has faced this problem where helper table (via db.table()) is not being created. There was one issue from someone where they had somewhat similar problem, but he was facing as he wanted to do across multiple database. I am doing on same database.

class Subscription(db.Model):
    __tablename__ = "subscription"
    id = db.Column(db.Integer, primary_key=True)
    subscription_name = db.Column(db.String(100), unique=True, nullable=False)


User_Subscription = db.table('user_subscription',
    db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
    db.Column('subscription_id', db.Integer, db.ForeignKey('subscription.id')),
    db.Column('subscription_status', db.String(20), nullable=False, default='Active'))

class Users(db.Model, UserMixin):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    firstname = db.Column(db.String(50), unique=False, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    date_created = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    date_updated = db.Column(db.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
    user_status_id = db.Column(db.String(2), db.ForeignKey('user_status.id'))
    subscriptions = db.relationship('Subscription', secondary=User_Subscription, backref= db.backref('subscriptions', lazy=True))

    def __repr__(self):
        return f"Users('{self.firstname}', '{self.email}')"

class User_Status(db.Model):
    __tablename__ = "user_status"
    id = db.Column(db.String(2), primary_key=True)
    status_desc = db.Column(db.String(20), unique=True, nullable=False)
    users_status = db.relationship('Users', backref='usersofstatus')

Expected Result - 4 tables to be created in database.

Actual Result - Only 3 tables are being created. User_Subscription (Helper table for many to many relationship) is not being created.

0

2 Answers 2

1

After few hours of frustration, i found that i had a typo, i was using db.table instead of db.Table. So posting this answer in case it can help someone.

Sign up to request clarification or add additional context in comments.

Comments

0

New to SQLAlchemy myself but it appears you are not inheriting from db.Model when creating User_Subscription. Why not make it a class?

class User_Subscription(db.Model): 

1 Comment

I am new to flask-sqlalchemy, so i read their official documentation at flask-sqlalchemy.pocoo.org/2.3/models/…, and it says "For this helper table it is strongly recommended to not use a model but an actual table:". Also, i was watching video youtube.com/…, they also not used db.Model. I am not sure why it is not creating that table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.