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

How to cover the original model by TimestampsMixin? #91

Open
vba34520 opened this issue Sep 8, 2022 · 3 comments
Open

How to cover the original model by TimestampsMixin? #91

vba34520 opened this issue Sep 8, 2022 · 3 comments

Comments

@vba34520
Copy link

vba34520 commented Sep 8, 2022

I have a table in MySQL like this

CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

Auto generate

pip install pymysql
pip install sqlacodegen

sqlacodegen mysql+pymysql://root:[email protected]:3306/test

Result

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    created_at = Column(DateTime)
    updated_at = Column(DateTime)
    deleted_at = Column(DateTime)

And I combine it with sqlalchemy_mixins

main.py

from flask import Flask
from sqlalchemy_mixins import AllFeaturesMixin
from sqlalchemy_mixins.timestamp import TimestampsMixin
from sqlalchemy import Column, Integer, String, DateTime
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:[email protected]:3306/test'
db = SQLAlchemy(app, session_options={'autocommit': True})


class Base(db.Model, AllFeaturesMixin, TimestampsMixin):
    __abstract__ = True


class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    created_at = Column(DateTime)
    updated_at = Column(DateTime)
    deleted_at = Column(DateTime)


Base.set_session(db.session)
db.create_all()

if __name__ == '__main__':
    user = User.create(name='Bob')
    print(user.to_dict())
    # {'id': 1, 'name': 'Bob', 'created_at': None, 'updated_at': None, 'deleted_at': None}

How to make created_at and updated_at are not None without deleting their statements?

@michaelbukachi
Copy link
Collaborator

Remove created_at and updated_at from the User model definition and it should work as intended.

@vba34520
Copy link
Author

First, Removing them manually will increase workload after auto generate.

Second, It will affect the original order of fields.

@vba34520
Copy link
Author

vba34520 commented Oct 8, 2022

How to achieve logical deleting by deleted_at? Thanks!

For example, a user record's deleted_at was set. I hope Product.find_or_fail(id) will raise sqlalchemy_mixins.ModelNotFoundError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants