GithubHelp home page GithubHelp logo

Comments (6)

long2ice avatar long2ice commented on August 26, 2024 1

Yes, that's good, and you can run make style ci before commit, thanks for contribution!

from aerich.

long2ice avatar long2ice commented on August 26, 2024

Can you show model difference before and after?

from aerich.

madnesspie avatar madnesspie commented on August 26, 2024

From migrations/old_models.py

# Before:
class ConfirmationCode(BaseAbstractModel, GetCredentialMixin):
    value = fields.CharField(
        max_length=8,
        default=lambda: ''.join(random.choice(string.digits) for i in range(8)),
        unique=True,
    )

    # Relations
    email = fields.ForeignKeyField(
        model_name="diff_models.Email",
        related_name="tokens",
        null=True,
    )
    phone = fields.ForeignKeyField(
        model_name="diff_models.Phone",
        related_name="tokens",
        null=True,
    )
    type = fields.ForeignKeyField(model_name="diff_models.ConfirmationCodeType")

    class Meta:
        table = "confirmation_code"

# After
class ConfirmationCode(BaseAbstractModel, GetCredentialMixin):
    value = fields.CharField(
        max_length=8,
        default=lambda: ''.join(random.choice(string.digits) for i in range(8)),
        unique=True,
    )
    fale_field_to_create_migration = fields.IntField(null=True)

    # Relations
    email = fields.ForeignKeyField(
        model_name="diff_models.Email",
        related_name="tokens",
        null=True,
    )
    phone = fields.ForeignKeyField(
        model_name="diff_models.Phone",
        related_name="tokens",
        null=True,
    )
    type = fields.ForeignKeyField(model_name="diff_models.ConfirmationCodeType")

    class Meta:
        table = "confirmation_code"

Generated migration:

{
  "upgrade": [
    "ALTER TABLE \"confirmation_code\" ALTER COLUMN \"value\" DROP DEFAULT",
    "ALTER TABLE \"confirmation_code\" ALTER COLUMN \"value\" TYPE VARCHAR(8)",
    "ALTER TABLE \"confirmation_code\" ADD \"fale_field_to_create_migration\" INT"
  ],
  "downgrade": [
    "ALTER TABLE \"confirmation_code\" ALTER COLUMN \"value\" DROP DEFAULT",
    "ALTER TABLE \"confirmation_code\" ALTER COLUMN \"value\" TYPE VARCHAR(8)",
    "ALTER TABLE \"confirmation_code\" DROP COLUMN \"fale_field_to_create_migration\""
  ]
}

from aerich.

ALexALed avatar ALexALed commented on August 26, 2024

Hi, thank you for the great tool! I have the same issue for the case where default=callable, I just wondering why the call for callable is omitted here?

if callable(default) or isinstance(field_object, (UUIDField, TextField, JSONField)):

Can we add more sophisticated handling for callables as defaults?

from aerich.

long2ice avatar long2ice commented on August 26, 2024

defaultgenerated in migrations is affected in database, obviously database can't support callable default.

from aerich.

ALexALed avatar ALexALed commented on August 26, 2024

@long2ice yes, you are right, sorry for the confusion, I said about change handling callable defaults in detect migrations, as an example this fix solve my issue:
Instead of:

if new_field.default != old_field.default:

I use :

                        if new_field.default != old_field.default and not callable(
                            new_field.default
                        ):

What do you think about this change? I can make PR if you are ok with this

The main point: we shouldn't detect changes for callables in migrations

from aerich.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.