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

how to fill initial data that contains foreign key reforence? #159

Open
MansonLuo opened this issue Dec 2, 2022 · 0 comments
Open

how to fill initial data that contains foreign key reforence? #159

MansonLuo opened this issue Dec 2, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@MansonLuo
Copy link

MansonLuo commented Dec 2, 2022

hi.

I have two tables: Tag and Note

`
class Tag extends ManagedObject<_Tag> implements _Tag {}
class _Tag {
@PrimaryKey
int? id;

@column(unique: true, indexed: true)
String? name;

ManagedSet? notes;
}

class Note extends ManagedObject<_Note> implements _Note {}
class _Note {
@PrimaryKey
int? id;

@column(indexed: true)
DateTime? createdTime;

@column(indexed: true)
DateTime? modifiedTime;

@RELATE(#notes)
Tag? tag;

@column(unique: true)
String? title;

String? content;

@column(defaultValue: 'false')
bool? isRecycle;
}

`

I got correct migration file when I run "conduit db generate".
following code is inserted into seed() method.

`

@OverRide
Future seed() async {
await database.store?.execute("INSERT INTO _Tag (name) VALUES (@name)",
substitutionValues: {
"name": "自助餐券",
});

await database.store?.execute(
    "INSERT INTO _Note (createdTime, modifiedTime, tag, title, content, isRecycle) VALUES (@createdTime, @modifiedTime, @tag, @title, @content, @isRecycle)",
    substitutionValues: {
      "createdTime": DateTime.now(),
      "modifiedTime": DateTime.now(),
      "tag": 0,
      "title": "标题",
      "content": "内容",
      "isRecycle": false,
    });

}

`

I got error when I run "conduit db update.."

-- Conduit CLI Version: 4.0.0
-- Conduit project version: 4.0.0
-- Updating to version 1 on new database...
PostgreSQL connecting, manson@localhost:5432/notes.
Initializating database...
CREATE TABLE _conduit_version_pgsql (versionNumber INT NOT NULL UNIQUE,dateOfUpgrade TIMESTAMP NOT NULL)
Applying migration version 1...
CREATE TABLE _Note (id BIGSERIAL PRIMARY KEY,createdTime TIMESTAMP NOT NULL,modifiedTime TIMESTAMP NOT NULL,title TEXT NOT NULL UNIQUE,content TEXT NOT NULL,isRecycle BOOLEAN NOT NULL DEFAULT false)
CREATE INDEX _Note_createdTime_idx ON _Note (createdTime)
CREATE INDEX _Note_modifiedTime_idx ON _Note (modifiedTime)
CREATE TABLE _Tag (id BIGSERIAL PRIMARY KEY,name TEXT NOT NULL UNIQUE)
CREATE INDEX _Tag_name_idx ON _Tag (name)
ALTER TABLE _Note ADD COLUMN tag_id BIGINT NULL
CREATE INDEX _Note_tag_id_idx ON _Note (tag_id)
ALTER TABLE ONLY _Note ADD FOREIGN KEY (tag_id) REFERENCES _Tag (id) ON DELETE SET NULL
Seeding data from migration version 1...
Query:execute (2ms) INSERT INTO _Tag (name) VALUES (@name) -> []
*** There was an issue. Reason: column "tag" of relation "_note" does not exist. Table: null Column: null

I change value of tag from 0 to String value "标签", but it still gave me same error.
but if I totally remove tag from inserting initial values, it got works with actual value of tag null.

[{"id":1,"createdTime":"2022-12-02T19:14:01.542438Z","modifiedTime":"2022-12-02T19:14:01.542438Z","title":"标题","content":"内容","isRecycle":false,"tag":null}]

I wonder if there is a way to fill this kind of initial data. could you give me any advise, thanks.

@MansonLuo MansonLuo added the enhancement New feature or request label Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant