angel-dart / angel

[ARCHIVED] A polished, production-ready backend framework in Dart for the VM, AOT, and Flutter.
https://angel-dart.dev/
MIT License
1.06k stars 67 forks source link

defaultValue results in SQL error #221

Closed thosakwe closed 3 years ago

thosakwe commented 4 years ago

This issue was originally created by @anondev32 here, before being automatically moved: https://github.com/angel-dart-archive/orm/issues/81


@SerializableField(defaultValue: 'empty description')
String get description;

builds to:

table.varChar('description')..defaultsTo('empty description');

Bringing up "/home/daniel/projects/riester/server/lib/src/models/car.g.dart#CarMigration"...
Unhandled exception:
PostgreSQLSeverity.error 42601: syntax error at or near "description" 
#0      _PostgreSQLConnection&Object&_PostgreSQLExecutionContextMixin._enqueue (package:postgres/src/connection.dart:402:24)
<asynchronous suspension>
#1      _PostgreSQLConnection&Object&_PostgreSQLExecutionContextMixin.execute (package:postgres/src/connection.dart:345:12)
#2      PostgresSchema.run (package:angel_migration_runner/src/postgres/schema.dart:14:61)
#3      PostgresMigrationRunner.up (package:angel_migration_runner/src/postgres/runner.dart:69:22)
<asynchronous suspension>
#4      _UpCommand.run (package:angel_migration_runner/src/cli.dart:25:28)
#5      CommandRunner.runCommand (package:args/command_runner.dart:196:27)
<asynchronous suspension>
#6      CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:111:29)
#7      new Future.sync (dart:async/future.dart:224:31)
#8      CommandRunner.run (package:args/command_runner.dart:111:11)
#9      runMigrations (package:angel_migration_runner/src/cli.dart:12:14)
#10     main (file:///home/daniel/projects/riester/server/bootstrap/migrations.dart:17:3)
<asynchronous suspension>
#11     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:298:32)
#12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)

When i remove ..defaultsTo('empty description'); from the generated file everything works perfectly.

thosakwe commented 4 years ago

@thosakwe commented:

Try "'empty description'" instead, and see if it works. I'm not sure off the top of my head if the migration tool automatically escapes strings.

thosakwe commented 4 years ago

@anondev32 commented:

  @SerializableField(defaultValue: "'empty description'")
  String get description;

build to

..defaultsTo('\'empty description\'');

results in

PostgreSQLSeverity.error 42601: syntax error at or near "\"

thosakwe commented 4 years ago

@anondev32 commented:

Sorry, i tried to dig into the migration and orm code, but couldn't find the part where it actually generates the SQL statements.

thosakwe commented 4 years ago

@thosakwe commented:

Can you turn in query logging in your PostgreSQL instance? The MigrationRunner API does not yet use QueryExecutor (it predates it), so you can't yet use the Dart logger.

I intend to do some maintenance work this weekend, but am in training all week.

thosakwe commented 4 years ago

@anondev32 commented:

Will do soon, keeping you updated.

I am actually very happy to help you with developing the framework/packages but have a lot going on right now. Will take some time for me to dig into the code.

thosakwe commented 4 years ago

@anondev32 commented:

..defaultsTo('\'empty description\'');

ERROR:  syntax error at or near "\" at character 109
2019-05-09 19:12:16.327 UTC [47] STATEMENT:  CREATE TABLE "cars" (
      "id" serial PRIMARY KEY,
      "make" varchar NOT NULL,
      "description" varchar DEFAULT \'empty description\',
      "family_friendly" boolean NOT NULL,
      "recalled_at" timestamp NOT NULL,
      "created_at" timestamp,
      "updated_at" timestamp
    );
thosakwe commented 4 years ago

@anondev32 commented:

defaultsTo('"empty description"')

2019-05-09 19:16:43.180 UTC [60] ERROR:  column "empty description" does not exist
2019-05-09 19:16:43.180 UTC [60] STATEMENT:  CREATE TABLE "cars" (
      "id" serial PRIMARY KEY,
      "make" varchar NOT NULL,
      "description" varchar DEFAULT "empty description",
      "family_friendly" boolean NOT NULL,
      "recalled_at" timestamp NOT NULL,
      "created_at" timestamp,
      "updated_at" timestamp
    );
thosakwe commented 4 years ago

@anondev32 commented:

..defaultsTo("'empty description'");

2019-05-09 19:18:33.384 UTC [64] ERROR:  syntax error at or near "\" at character 109
2019-05-09 19:18:33.384 UTC [64] STATEMENT:  CREATE TABLE "cars" (
      "id" serial PRIMARY KEY,
      "make" varchar NOT NULL,
      "description" varchar DEFAULT \'empty description\',
      "family_friendly" boolean NOT NULL,
      "recalled_at" timestamp NOT NULL,
      "created_at" timestamp,
      "updated_at" timestamp
    );
thosakwe commented 4 years ago

@anondev32 commented:

I found the issue, will create a pull request asap.