-
Notifications
You must be signed in to change notification settings - Fork 131
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
problem with db.sync() #283
Comments
This is the code casuing the crash:
|
bump |
This is likely because you have not specified a type for /* … */
id: {
...DataTypes.string(36),
primaryKey: true
},
/* … */ |
That code uses ES spread syntax. If you don’t want to use that, do this: /* … */
id: {
type: DataTypes.STRING,
length: 36,
primaryKey: true
},
/* … */ |
As far as I can tell, the documentation also seems to describe putting defaults in their own static field, like this: /* … */
static defaults = {
id: "00000000-0000-0000-0000-000000000000"
}
/* … */ |
Note that it is likely not a good idea to specify a default ID. While your code may look like it generates a random UUID for every entry, it actually generates one UUID when the model is defined, which is used throughout. It may even be invalid to provide a default primary key. |
That’s not how GH issues work |
is there any kind of "beforeCreate" hook to populate an id field? or should I do that in my controller. |
I also still get an error with
|
I have the same issue, |
What is the error you get when you run sync? |
I have 3 models username is a column from users table and it contains a constraint to guarantee that is unique. |
I think sync() should verify first if the table exists or not then creates it. |
I think that's what it's doing under the hood (
async sync(options: SyncOptions = {}) {
if (options.drop) {
for (let i = this._models.length - 1; i >= 0; i--) {
await this._models[i].drop();
}
}
...
for (const model of this._models) {
await model.createTable();
}
}
static async createTable() {
....
const createQuery = this._options.queryBuilder
.queryForSchema(this)
.table(this.table)
.createTable(
this.formatFieldToDatabase(this.fields) as ModelFields,
this.formatFieldToDatabase(this.defaults) as ModelDefaults,
{
withTimestamps: this.timestamps,
ifNotExists: true,
},
)
.toDescription();
await this._options.database.query(createQuery);
....
} I'd be interested to see how I could reproduce the error you're seeing, if you'd care to share some code snippets |
I took a look before at that function. import { DataTypes, Model } from "https://deno.land/x/[email protected]/mod.ts";
import {
Database,
PostgresConnector,
} from "https://deno.land/x/[email protected]/mod.ts";
class UserModel extends Model {
static table = "users";
static timestamps = true;
static fields = {
id: { primaryKey: true, autoIncrement: true, type: DataTypes.INTEGER },
username: { type: DataTypes.STRING, unique: true },
email: DataTypes.STRING,
password: DataTypes.STRING,
};
}
class ProductModel extends Model {
static table = "products";
static timestamps = true;
static fields = {
id: { primaryKey: true, autoIncrement: true, type: DataTypes.INTEGER },
name: { type: DataTypes.STRING, unique: true },
quantity: DataTypes.INTEGER
};
}
const connector = new PostgresConnector({
database: "db",
host: "127.0.0.1",
username: "postgres",
password: "postgres",
port: 5432,
});
const db = new Database(connector);
const syncDB = async () => {
db.link([UserModel, ProductModel])
await db.sync()
} First time, it will work correctly but if you restart the server or reload it with denon you will get an issue. This is the error I get:
|
Ah yeah, it looks like the table creation generates two commands:
and
The second one is the one that's failing. Seems like we could check if the constraint already exists before adding it to the query - but I'd guess it'd be easier for denodb to implement some form of migrations instead of building up the sync method... |
Thanks for that, so I need to do a workaround to accomplish that until it will be taken into consideration in the future. |
I created a migration file to accomplish my task (it's not clean but it does the work) and I think that the sync method is overengineering and contains a lot of logic that makes things not easy to do. |
(Fine, I'll do it myself...) This commit represents a branch to replace the denodb orm with deno-postgres and a small db provider to access it. Current DenoDB project had many issues and specially had some troubles sync the db without dropping the info: eveningkid/denodb#283, eveningkid/denodb#258 So everytime the server restarted, the db was wiped out and therefore it was unusable for production. After this change, the db is now "persistent" (and the code is arguably cleaner/straightforward)
error: Uncaught (in promise) SqliteError: index email already exists
I have a subscriptions table with email as a field.
Using
db.sync()
causes this error. I'm not sure if I need it. It appears to work without it, but I'm not sure.Can someone clarify the point of
db.sync()
The text was updated successfully, but these errors were encountered: