Faild to run migrations after adding new columns to Table #594
-
Hi. Here is my code:
I tried manually add this changes to my first migration file, but it doesn't work too. So I think there should be a way to delete old table and create the new one (with the new columns), because it seems that piccolo remembered an old table and it tries to create a new table instead of adding new columns, and that's why he says relation "student" already exists. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
What you've done should work OK. Your first migration file should look something like this (note how there's an from piccolo.apps.migrations.auto.migration_manager import MigrationManager
from piccolo.columns.column_types import Integer
from piccolo.columns.column_types import Varchar
from piccolo.columns.indexes import IndexMethod
ID = "2022-08-19T23:36:27:820870"
VERSION = "0.85.1"
DESCRIPTION = ""
async def forwards():
manager = MigrationManager(
migration_id=ID, app_name="home", description=DESCRIPTION
)
manager.add_table("Student", tablename="student")
manager.add_column(
table_class_name="Student",
tablename="student",
column_name="last_name",
db_column_name="last_name",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 100,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)
manager.add_column(
table_class_name="Student",
tablename="student",
column_name="first_name",
db_column_name="first_name",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 100,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)
manager.add_column(
table_class_name="Student",
tablename="student",
column_name="phone_number",
db_column_name="phone_number",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 100,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)
manager.add_column(
table_class_name="Student",
tablename="student",
column_name="cohort_number",
db_column_name="cohort_number",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)
return manager And the second should look something like this (note how there's no from piccolo.apps.migrations.auto.migration_manager import MigrationManager
from piccolo.columns.column_types import Integer
from piccolo.columns.column_types import Varchar
from piccolo.columns.indexes import IndexMethod
ID = "2022-08-19T23:36:48:327594"
VERSION = "0.85.1"
DESCRIPTION = ""
async def forwards():
manager = MigrationManager(
migration_id=ID, app_name="home", description=DESCRIPTION
)
manager.add_column(
table_class_name="Student",
tablename="student",
column_name="personal_email",
db_column_name="personal_email",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 255,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)
manager.add_column(
table_class_name="Student",
tablename="student",
column_name="personal_id",
db_column_name="personal_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)
manager.add_column(
table_class_name="Student",
tablename="student",
column_name="personal_login",
db_column_name="personal_login",
column_class_name="Varchar",
column_class=Varchar,
params={
"length": 255,
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
)
return manager To make Piccolo delete a table, unregister it from APP_CONFIG = AppConfig(
...
table_classes=table_finder(modules=["home.tables"]),
) Then just comment out the You might want to use a tool like PgAdmin too so you can inspect the current state of the database. |
Beta Was this translation helpful? Give feedback.
What you've done should work OK.
Your first migration file should look something like this (note how there's an
add_table
and severaladd_column
):