I'm learning Entity Framework Core(everything is last version), at first lesson when i'm creating a db from a class.
after "Add-Migration v1" is "Build succeeded." - everything is working, i am adding data and fetching it.
From the lesson i should "Update-Database v1" and here is the problem- error.
Failed executing DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Users] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NULL,
[Age] int NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY ([Id])
);
"here is already an object named 'Users' in the database. "
This error didn't bother me till everything is working, but at lesson nr 2 when we should change variable types, add columns ... only Add-Migration Vx is not working without Update-Database Vx, so to solve, for every update, i delete db and Migrations folder and going along with lessons only with Add-Migration Vx - also working.
It is a way to update the changes without Update-Database Vx* ? if someone know how i could solve the error of "here is already an object named ... in the database. " thanks
Related
I am running a script that creates a database, some tables with foreign keys and insert some data, but somehow creating the tables is not working, although it doesn't throw any error: I go to pgAdmin, look for the tables created and there's no one...
When I copy the text of my script and execute it into the Query Tool, it works fine and creates the tables.
Can you please explain me what I am doing wrong?
Script:
DROP DATABASE IF EXISTS test01 WITH (FORCE); --drops even if in use
CREATE DATABASE test01
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'German_Germany.1252'
LC_CTYPE = 'German_Germany.1252'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
IS_TEMPLATE = False
;
CREATE TABLE customers
(
customer_id INT GENERATED ALWAYS AS IDENTITY,
customer_name VARCHAR(255) NOT NULL,
PRIMARY KEY(customer_id)
);
CREATE TABLE contacts
(
contact_id INT GENERATED ALWAYS AS IDENTITY,
customer_id INT,
contact_name VARCHAR(255) NOT NULL,
phone VARCHAR(15),
email VARCHAR(100),
PRIMARY KEY(contact_id),
CONSTRAINT fk_customer
FOREIGN KEY(customer_id)
REFERENCES customers(customer_id)
ON DELETE CASCADE
);
INSERT INTO customers(customer_name)
VALUES('BlueBird Inc'),
('Dolphin LLC');
INSERT INTO contacts(customer_id, contact_name, phone, email)
VALUES(1,'John Doe','(408)-111-1234','john.doe#bluebird.dev'),
(1,'Jane Doe','(408)-111-1235','jane.doe#bluebird.dev'),
(2,'David Wright','(408)-222-1234','david.wright#dolphin.dev');
I am calling the script from a Windows console like this:
"C:\Program Files\PostgreSQL\15\bin\psql.exe" -U postgres -f "C:\Users\my user name\Desktop\db_create.sql" postgres
My script is edited in Notepad++ and saved with Encoding set to UTF-8 without BOM, as per a suggestion found here
I see you are using -U postgres command line parameter, and also using database name as last parameter (postgres).
So all your SQL commands was executed while you are connected to postgres database. Of course, CREATE DATABASE command did creation of test01 database, but CREATE TABLE and INSERT INTO did executed not for test01 database, but for postgres database, and all your tables are in postgres database, but not in test01.
You need to split your SQL script into 2 scripts (files): first for 'CREATE DATABASE', second for the rest of.
You need to execute first script as before, like
psql.exe -U postgres -f "db_create_1.sql" postgres
And for second one need to choose the database which was created at 1st step, like
psql.exe -U postgres -f "db_create_2.sql" test01
I have scripts
/*The extension is used to generate UUID*/
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- auto-generated definition
create table users
(
id uuid not null DEFAULT uuid_generate_v4 ()
constraint profile_pkey
primary key,
em varchar(255),
user varchar(255)
);
In IDE Intellij Idea (a project with Spring Boot):
src/main/resources/db-migration
src/main/resources/sql_scripts :
copy.sql
user.txt
I'm just trying to run a simple Sql command for now to see that everything works clearly
copy.sql
COPY profile FROM '/sql_scripts/user.txt'
USING DELIMITERS ',' WITH NULL AS '\null';
user.txt
'm#mai.com', 'sara'
's#yandex.ru', 'jacobs'
But when I run the copy command, I get an error
ERROR: could not open file...
Maybe who knows how it should work and what needs to be fixed ?
Strong possibility its a pathing issue; could you try, instead of
COPY profile FROM '/sql_scripts/user.txt'
doing
COPY profile FROM './sql_scripts/user.txt'
(or an absolute path)
I'm trying to build a table with csvsql.
When I use command:
csvsql --db mysql://user:password#localhost:3306/database_name --table table_name file.csv
I get the error:
(in table 'blabla', column 'xyz'): VARCHAR requires a length on dialect mysql
I've then tried to build a database schema and force it with --db-schema flag,
The db-schema format is:
CREATE TABLE table_name (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`x` varchar(29) DEFAULT NULL,
`y` int(10) NOT NULL DEFAULT '0',
`z` BOOL NOT NULL,
PRIMARY KEY (`id`),
KEY `indexed` (`indexed`)
);
but I still get the same error.
The complete command with db-schema is:
csvsql --db mysql://user:password#localhost:3306/database_name --table table_name --db-schema db_schema_filename csvfile.csv
I've read the manual for csvkit, but I don't get what I'm doing wrong.
This command should print the conversion result right?
Can someone please help?
Thank you.
Well, found the solution in the github.
https://github.com/wireservice/csvkit/issues/758#issue-201924611
After update from github, no more errors and tables are created normaly.
When I run a Entity Framework migration on deploying a asp.net web app I get this error:
[SqlException (0x80131904): Incorrect syntax near ':'.]
My sql scripts starts with:
:On Error Exit
SET XACT_ABORT ON
GO
Begin Transaction
set ANSI_WARNINGS OFF
INSERT [dbo].[Test] ([ID], [Key], [Parent]) VALUES (1, N'bla', NULL)
...
When I run this script with SqlCMD it works fine of course, because this script was optimized for SqlCMD.
But it does not work when entity frameworks 6 SqlFile("scriptfile.sql"); method is executed because the very first ":" seems to be the problem. But I need that ':On Error Exit' directive because of SqlCMD.exe.
My final goal is to have a sql file with many sql inserts wrapped by a transaction which aborts when any error occurs and this should work for SqlCMD and EF 6 SqlFile method.
How can I fix that?
Using MVC4 and EF Code First
I renamed my table/column-name i.e. table: from Categories to Category in the model and in the code and when I run the migration statement
Update-Database -Verbose -Force
I get an error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.Categories' and the index name 'PK_dbo.Categories'. The duplicate key value is (). Could not create constraint. See previous errors. The statement has been terminated.
In the configuration file I have AutomaticMigrationsEnabled = true;
Is there something else I need to do to make the changes apply in the database?
You cannot modify the PK.
1 - Delete existing database then run Update-Database
2 - Generate update script by running Update-Database -Script and add drop index TSQL to generated file and run the script.
3 - Remove key constrain from table via Visual Studio or Management Studio then remove TSQL for dropping index in generated script then run script.