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

Postgres sql sentences with syntax error #1480

Closed
orlando-rojas opened this issue Feb 22, 2021 · 1 comment · Fixed by #1641
Closed

Postgres sql sentences with syntax error #1480

orlando-rojas opened this issue Feb 22, 2021 · 1 comment · Fixed by #1641
Assignees

Comments

@orlando-rojas
Copy link

Describe the bug
I have the following entity

import { Entity, PrimaryKey, Property } from "@mikro-orm/core";

@Entity()
export class Post {
  @PrimaryKey()
  id!: number;

  @Property({ type: "date" })
  createdAt: Date = new Date();

  @Property({ type: "date", onUpdate: () => new Date() })
  updatedAt: Date = new Date();

  @Property({ type: "text" })
  title!: string;
}

and this code in my index.ts file

import { MikroORM } from "@mikro-orm/core";
import { Post } from "./entities/Post";
import microConfig from "./mikro-orm.config";

const main = async () => {
  const orm = await MikroORM.init(microConfig);
  await orm.getMigrator().up();
  const post = orm.em.create(Post, { title: "my frist post" });
  await orm.em.persistAndFlush(post);
};

main()

After creating the migration with npx mikro-orm migration:create
I get the following migration

import { Migration } from "@mikro-orm/migrations";

export class Migration20210222221432 extends Migration {
  async up(): Promise<void> {
    this.addSql(
      'alter table "post" drop constraint if exists "post_id_check";'
    );
    this.addSql(
      'alter table "post" alter column "id" type serial primary key using ("id"::serial primary key);'
    );
  }
}

and when I try to run my index.ts file I get this error

SyntaxErrorException: alter table "post" alter column "id" type serial primary key using ("id"::serial primary key); - syntax error at or near "primary"
@B4nan
Copy link
Member

B4nan commented Feb 22, 2021

That code would not create such migration on its own - you had to have the column defined before wrongly - your problematic migration is an alter statement.

In other words, this is not enough to replicate it.

Moreover, this sounds more like a knex issue, as that is where the actual query gets generated (and where the problematic type casting happens).

Migrations are made this way so you have absolute control - so to work this around, fix the query manually in the generated file.

B4nan added a commit that referenced this issue Apr 16, 2021
This commit introduces new schema diffing capabilitites. Instead of shared code for
creating and altering tables, we now build schema objects from entities and existing
schema, and compare those (this will later allow to create down migrations too).

- Better mapping - adds mapped types to abstract most of the column types (e.g. { type: t.decimal })
- FK diffing
- Proper index diffing (before we compared just names)
- Custom index expressions
- Comment diffing
- Column length diffing (e.g. numeric(10,2) or varchar(100))
- Changing PK types
- Schena/namespace diffing (posgtres only)

Closes #1486
Closes #1518
Closes #579
Closes #1559
Closes #1602
Closes #1480
Closes #1687
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

Successfully merging a pull request may close this issue.

2 participants