Skip to content

Adding a column drops the whole table #2000

Closed

Description

@lukehutch

I got the following migration warning:

One or more columns are added to table "social_post" which cannot be added in a table migration.
The complete table will be deleted and recreated.

I just added one bool column. Nothing special. Why on earth did the whole table have to be dropped? Is this because of the lack of default values (#1140)? If so, this is crazy -- #1140 needs to be implemented.

Activity

  1. craiglabenz commented on Mar 10, 2024

    @craiglabenz
    Contributor

    I saw this, too @lukehutch. It's possible that you added a NOT NULL column, which the migration system can of course not handle. (I'm actually not sure if this is WHY Serverpod defaults to that behavior).

    I got around this by rewriting the generated migration.sql file to instead add a nullable column, seed values into every row, and then modify the schema to add a NOT NULL constraint.

    A key thing to remember is that you're free to completely rewrite the initial SQL in migration.sql as needed.

  2. lukehutch commented on Mar 10, 2024

    @lukehutch
    ContributorAuthor

    Not really, because the next time you try to generate a migration, Serverpod will try to revert any changes you manually made in the previous migration.

    The reason Serverpod does this is because there is no support for default values for non-bulla le columns yet.

  3. SandPod commented on Mar 11, 2024

    @SandPod
    Contributor

    Is this because of the lack of default values (#1140)?

    This is correct. For any database, if you would like to add a non nullable column to a table a default value would be required. And since we don't support default values in the framework yet, we can't support adding a non nullable column in a single migration step.

    This is an important feature we are trying to prioritize.

    The current recommended workflow given the limitation of not having a default values is:

    1. Add the column as nullable in the model, create a migration and apply it to the database.
    2. Seed the column in whatever way possible (either you could add SQL code to the migration that adds the column or you could manually seed it some way through direct queries).
    3. Drop the nullability of the column in the model, create a migration using the --force flag and apply it to the database.

    We still display a warning on step 3 since the migration will fail if all entries don't have a value at that state.

    Will close this issue as this is a consequence of lacking support for default values that is already tracked here: #1140.
    And this specific case is already identified as a comment in the other issue: #1140 (comment)

  4. develocode777 commented on Mar 11, 2024

    @develocode777

    This solution does not work. It still shows the warning after manually adding the column, seeding it and making it non nullable.

  5. SandPod commented on Mar 11, 2024

    @SandPod
    Contributor

    We still display a warning on step 3 since the migration will fail if all entries don't have a value at that state.

    The --force flag is required in step 3 since we still display a warning.

  6. SandPod commented on Mar 11, 2024

    @SandPod
    Contributor

    Sorry, I understand the confusion now. I will update the instructions to make the steps a bit more clear.

  7. develocode777 commented on Mar 11, 2024

    @develocode777

    Ah, I get it now, it works, thanks.

  8. shawnAshton commented on Feb 5, 2026

    @shawnAshton

    @SandPod
    What is the recommended workflow for adding a new relation?

    "The "nullable" property is not allowed for relation type. Valid keys are {parent, field, onUpdate, onDelete, optional, name}." - serverpod error in the spy.yaml

    I also attempted by adding "optional" to the relation, but that also is requiring me to drop the table.

  9. marcelomendoncasoares commented on Feb 6, 2026

    @marcelomendoncasoares
    Collaborator

    I also attempted by adding "optional" to the relation, but that also is requiring me to drop the table.

    @shawnAshton Have you tried creating the relation with optional and specifying the foreign key field, so that you can set the field as nullable explicitly?

    # user.yaml
    class: User
    table: user
    fields:
      customIdField: int?  # <-- Set this field
      address: Address?, relation(optional, field=customIdField)

    Although this seems more like a bug, since making the object relation optional should result in this underneath. Could you test if this work and open a dedicated issue for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions